1
  1. Does QuerySnapshot only return added/updated/removed documents or all documents, not only updated ones, from Firestore?

  2. If it returns all the documents, then is there any way to get only newly added/updated/removed documents?

  3. What is the difference between getDocuments() and getDocumentChanges() in Firestore's QuerySnapshot and when to use them?

  4. 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.

1 Answers1

2
  1. Does QuerySnapshot only return added/updated/removed documents or all documents(not only updated ones) from Firestore?

A QuerySnapshot object contains all the results of a Firestore query. So if you perform a query in Firestore, all the results that are returned can be found in the QuerySnapshot object.

  1. If it returns all the documents then is there any way to get only newly added/updated/removed documents?

Yes, there is. You use an addSnapshotListener(EventListener listener) to listen for updates in real-time. This means that can always view changes between snapshots. So you can be notified if a document is only added, modified, or removed. But please also note, that there is no way you can skip the initial retrieval of the documents.

  1. What is the difference between getDocuments() and getDocumentChanges() in Firestore's QuerySnapshot and when to use them?

The getDocuments() method:

Returns the documents in this QuerySnapshot as a List in order of the query.

While getDocumentChanges() method:

Returns the list of documents that changed since the last snapshot.

So each method does a different operation.

Edit:

  1. 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?

It will get all documents when you are first opening the app, and unfortunately, this behavior cannot be changed. Right after that, each method will fire according to the performed operation. For example, if a document is added, you'll only get a single document in the ${dc.document.data}.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thank you for your answer. But I am confused about 2nd question's answer. I edited the question. Please check it. – Shyamaly Lakhadive Nov 26 '21 at 08:20
  • Please check my updated answer. Is it clear now? – Alex Mamo Nov 26 '21 at 08:35
  • Once again I updated my question with more details. Please correct me if I am wrong. Thank you – Shyamaly Lakhadive Nov 26 '21 at 09:09
  • Yes, that's the expected behavior since you are getting **all** documents and **not** only the ones that have changed using [view changes between snapshots](https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots), right? – Alex Mamo Nov 26 '21 at 09:20
  • You have to check that in `DocumentChange.Type.MODIFIED -> Log.d(TAG, "Modified city: ${dc.document.data}")`. – Alex Mamo Nov 26 '21 at 09:21
  • Ok Got it. Originally we will get all the documents. `DocumentChange.Type.MODIFIED -> Log.d(TAG, "Modified city: ${dc.document.data}")` is just to get more specific document. Thank you for clearing doubt. – Shyamaly Lakhadive Nov 26 '21 at 09:25