saricden
saricden

Reputation: 2332

Performant way to listen for updates to large groups of documents in Firestore?

What is the most performant way of using onSnapshot to listen for changes to a large group of documents in Cloud Firestore?

(There would probably be a max of around 50-75 documents being returned by the query, paged with a 'load more' button which uses query.endBefore)

Would it be better to listen for changes to the entire query, or to query once and attach onSnapshot listeners to each document returned?

Note the documents aren't likely to change THAT often, but I still need to be able to listen for when they do.

Upvotes: 2

Views: 1217

Answers (1)

Todd Kerpelman
Todd Kerpelman

Reputation: 17523

You're better off listening to changes to the entire query.

Cloud Firestore is pretty efficient when it comes to sending changes over the network. So if you were to create a snapshot listener for a group of 75 documents and one of them changes, Cloud Firestore will only send your device that one changed document. (You'll still receive the full set of data in your snapshot listener, which is generally what you want. Firebase does the work of merging the new data into your cached data.)

Setting up 75 different snapshot listeners, one for each document, doesn't really save you anything in terms of network or battery costs. (In fact, it probably is less efficient on the client.) But it does make life a lot more difficult for you, and also means you'll miss events like new documents being added to your collection.

Upvotes: 5

Related Questions