I using Firestore for my flutter's app. I have two collections with with several hundred documents (up to 600). I need to notify the application user when a document changes. first I used this code:
_collectionRef.snapshots(includeMetadataChanges: true).listen((event) {
event.docChanges.forEach((change) {
if (change.type == DocumentChangeType.modified) {
print(change.doc.id);
}
});
but then I thought how optimal it is to keep track of hundreds of documents in two collections.
I got familiar with the best practices for Firestore. Found it:
Limit snapshot listeners per client 100
Keep the number of snapshot listeners per client under 100.
does this mean no more than 100 snapshots().listen in one application? did I understand it correctly? Or is it a limitation on the number of documents that the listener will listen to?
Is it normal practice to change hundreds of documents? Or is it better to change the data structure and limit the number of listened documents?
CodePudding user response:
The best practice you quote is to limit the number of listeners per application to at most 100. So that is the number of times you have an active call to .listen
, onSnapshot
or similar APIs. This recommendation is not related to the number of collections or documents that you listen to, although there may be separate recommendations for that.
If you want to notify the user when there is a change in a collection, I typically add a lastUpdated
timestamp to each document in that collection that you update on every write to that document. You can then use that field in queries, for example to get just the last modified document for the use-case you describe.