Does
QuerySnapshot
only return added/updated/removed documents or all documents, not only updated ones, from Firestore?If it returns all the documents, then is there any way to get only newly added/updated/removed documents?
What is the difference between
getDocuments()
andgetDocumentChanges()
in Firestore'sQuerySnapshot
and when to use them?In the below code, is it returning all documents or only added/modified/removed documents? Because It's like we are getting all documents and then sorting them according to their state. Is it correct?
.addSnapshotListener { snapshots, e -> if (e != null) { Log.w(TAG, "listen:error", e) return@addSnapshotListener } for (dc in snapshots!!.documentChanges) { when (dc.type) { DocumentChange.Type.ADDED -> Log.d(TAG, "New city:${dc.document.data}") DocumentChange.Type.MODIFIED -> Log.d(TAG, "Modified city: ${dc.document.data}") DocumentChange.Type.REMOVED -> Log.d(TAG, "Removed city: ${dc.document.data}") } } }
Edit:
Here is my code
.addSnapshotListener { value, error ->
if (error != null) {
cancel(
message = "Error fetching posts",
cause = error
)
return@addSnapshotListener
}
if (value != null) {
Log.d(TAG, "main value: ${value.size()}")
for (dc in value.documents) {
Log.d(TAG, "dc ${dc.data}")
}
offer(reaction)
}
}
Initially, when the app is open I am getting all documents and it's ok. But when I am modifying one document still I am getting all documents(Log.d(TAG, "main value: ${value.size()}")
answer is 2. I have a total of 2 documents right now so I am getting both documents modified and not modified). Means first I will get All documents and then I will sort them by using getDocumentChanges()
.
My code is in Kotlin.