RmK
RmK

Reputation: 1438

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.

Upvotes: 12

Views: 6114

Answers (3)

jeber
jeber

Reputation: 1

I'm doing something like this to track the local state

let localCollection = {};

        if(change.type == 'added')
        {
            localCollection[change.doc.ref.id] = change.doc;
        }
        if(change.type == 'modified') {
        let changeObject = {};
        console.log("Before", localCollection[change.doc.ref.id].data());
        changeObject.before = localCollection[change.doc.ref.id];
        changeObject.after = change.doc;
        console.log("After", change.doc.data());

Upvotes: 0

おだのぶなが
おだのぶなが

Reputation: 1

// flag
let flag = false;

collectionRef.onSnapshot((snapshot) => {
snapshot.docChanges().forEach((change) => {

    // NOTE: ctrl first execute
    if (!flag) {
        console.log('not initilized');
        return;
    }

    // anything
    console.log('only after initilized');
}, (err) => {
    console.log(err);
});

// set flag true 
if (!flag) {
    flag = true
    console.log('listener initilized!!');
};
});

Upvotes: -1

Doug Stevenson
Doug Stevenson

Reputation: 317372

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.

Upvotes: 4

Related Questions