Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentate between first time querysnapshot and change listener in firestore

I'm following the documentation of firestore here and trying to attach a snapshot listener on a collection for getting realtime updates.

I'm having trouble trying to differentiate between whether a response from the Snapshot event listener is a first time response (in which case all the documents in the collection will be returned) or a change event response in which case I want to add the check to identify what change happened. This footnote in the same documentation which goes as :

Important: The first query snapshot contains added events for all existing documents that match the query. This is because you're getting a set of changes that bring your query snapshot current with the initial state of the query

doesn't mention how to identify a first query snapshot with that of subsequent ones.

Only related question I can find on SO is this one but it wouldn't help in my case.

Any help is really appreciated since I've run out of directions to go to.

Thanks.

like image 913
RmK Avatar asked Apr 12 '18 14:04

RmK


People also ask

What is QuerySnapshot in firebase?

A QuerySnapshot contains zero or more QueryDocumentSnapshot objects representing the results of a query. The documents can be accessed as an array via the docs property or enumerated using the forEach method. The number of documents can be determined via the empty and size properties.

Which of the following method is used to delete a field from a particular document?

For deleting a specific field from a document, use the FieldValue. delete() method when we update a document.

How do I unsubscribe from onSnapshot?

You don't have to create a new listener to unsubscribe. Every time you call onSnapshot() the return value is a handler you can invoke to unsubscribe that listener later. That snippet is meant to show two parts of your app with the "//..." indicating other code in between.


1 Answers

Take a careful look at the code in the docs you referenced. It's checking the Type of each DocumentChange object in the QuerySnapshot object:

for (DocumentChange dc : snapshots.getDocumentChanges()) {
    switch (dc.getType()) {
        case ADDED:
            Log.d(TAG, "New city: " + dc.getDocument().getData());
            break;
        case MODIFIED:
            Log.d(TAG, "Modified city: " + dc.getDocument().getData());
            break;
        case REMOVED:
            Log.d(TAG, "Removed city: " + dc.getDocument().getData());
            break;
    }
}

This goes along with the text you cited:

The first query snapshot contains added events for all existing documents that match the query.

You can tell if you've seen a document for the first time because it's an ADDED type of change. MODIFIED and REMOVED type changes are only issued for documents you've seen previously for this listener.

like image 189
Doug Stevenson Avatar answered Nov 03 '22 09:11

Doug Stevenson