Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

android - How to query fields which has a FieldValue.ServerTimestamp value in it in firestore?

I'm using firestore in my android application. i'm setting below data class to my document,

data class User(
val uid:String = "",
val address:String = "",
@ServerTimestamp
val upTime: Date? = null
)

In my viewmodel init code im listening to following query,

val filterTime = System.currentTimeMillis() - 7*24*60*60*1000 // Listens to last 7 days of data;
val onlineQueryListener = db.collection("users")
.orderBy("upTime", Query.Direction.DESCENDING)
.whereGreaterThan("uptime", Date(filterTime))
.addSnapShotListener{value,error ->
 // do some work
}

On button click, sending new data to server,

val user = User("UserUidComesHere","some address")
db.collection("user").document(user.uid).set(user)

After a button click, the query listener is triggered when online, because the server timestamp will be written with time higher than the filterTime, but when the user is offline, the upTime value will be null and is not getting captured in the query "whereGreaterThan".

so i've added another listener to my init function to listen to the pending wirtes data by adding query for whereEqualTo null for field upTime, but it is not getting triggered.

val offlineQueryListener = db.collection("users")
    .orderBy("uptime", Query.Direction.DESCENDING)
    .whereEqualTo("uptime", null)
    .addSnapShotListener{value,error ->
     // do some work
    }

Above listener is never triggered. How to overcome the above problem. i wanted to capture null values in the FieldValue.ServerTimeStamp() fields.

question from:https://stackoverflow.com/questions/66063951/how-to-query-fields-which-has-a-fieldvalue-servertimestamp-value-in-it-in-firest

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...