Home > Blockchain >  Does QuerySnapshot only returns added/updated/removed documents or all documents from Firestore in k
Does QuerySnapshot only returns added/updated/removed documents or all documents from Firestore in k

Time:11-27

  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.

CodePudding user response:

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

  • Related