How to Skip Initial Data and Trigger Only New Updates in Firestore Firebase

How to skip initial data and trigger only new updates in Firestore Firebase?

When you are listening for changes in Cloud Firestore for realtime changes, using Firestore Query's addSnapshotListener() method, it:

Starts listening to this query.

Which basically means that first time you attach the listener, you get all documents that correspond to that particular query. Furthermore, everytime a property within a document changes, you are notified according to that change. Obviously, this is happening only if the listener remains active and is not removed.

Unfortunately, Firestore listeners don't work that way, so you cannot skip that "case ADDED". What you can do instead, is to add add under each user object a Date property (this is how you can add it) and query your database on client, according to this new property, for all documents that have changed since a previous time.

According to Nick Cardoso's comment, for future visitors that might ask why this behaviour happens, is because the reason he mentioned in his comment. I also recommend see Doug Stevenson's answer from this post, for a better understanding.

How to skip initial data from onSnapshot listener in Firestore Firebase?

Firestore listeners return the data that you request, and then listen for updates to that data. There is no way to skip the initial data and only receive the updates, but you can typically change what data you request.

For example, if you ensure each document in your collection has a timestamp of when it was created, you can use a query to only request data that was created after you start listening with:

collectionRef.where("timestamp", ">=", firebase.firestore.Timestamp.fromMillis(Date.now()));

How to skip initial data and trigger only new updates in Firestore Firebase?

When you are listening for changes in Cloud Firestore for realtime changes, using Firestore Query's addSnapshotListener() method, it:

Starts listening to this query.

Which basically means that first time you attach the listener, you get all documents that correspond to that particular query. Furthermore, everytime a property within a document changes, you are notified according to that change. Obviously, this is happening only if the listener remains active and is not removed.

Unfortunately, Firestore listeners don't work that way, so you cannot skip that "case ADDED". What you can do instead, is to add add under each user object a Date property (this is how you can add it) and query your database on client, according to this new property, for all documents that have changed since a previous time.

According to Nick Cardoso's comment, for future visitors that might ask why this behaviour happens, is because the reason he mentioned in his comment. I also recommend see Doug Stevenson's answer from this post, for a better understanding.

Does QuerySnapshot only returns added/updated/removed documents or all documents from Firestore in kotlin?

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

Listen to realtime updates in Firestore without downloading all initial data

Queries listeners always receive all matching documents that would be matched by the query. You can't change that. The only thing you can change is your query. Currently, your query is asking for all documents in the collection, and you will have to narrow that down with a filter.

What you can do instead is make sure your query only requests as much data as it wants. If your documents have a natural time ordering, try attaching a timestamp to each of the documents, and querying for only the documents that are new since the last time you performed the query. If you don't have some sort of "checkpoint" to figure out what's new vs old, then you'll have a hard time making this work out.

Disable the first query snapshot when adding a snapshotListener

There is no way to suppress getting the initial data.

Depending on the use-case, you may want to attach your listener to a subset of the data. E.g. if you have an append-only scenario (such as chat), you can start listening for data that was modified after "now".

Is there a way to suppress Firebase Functions Firestore triggers?

  1. No. Functions are always active when they're deployed. You would have delete it to stop it from working, or send it some other signal that you build yourself.

  2. No, user information is not available in the trigger. The best you can do is have the user write their UID to the document to find out who it was, and make sure it's correct in security rules.



Related Topics



Leave a reply



Submit