简体   繁体   中英

Does Firestore snapshost() listeners do an initial document read every data every time the app restarts?

When using firestore snapshot() , and set a listener, Cloud Firestore sends your listener an initial snapshot of the data, and then another snapshot each time the documents change.

However if I close the app, and reopen it, does firestore make a read on all the data it already has queried or is there an internal sync system (for example if they store documents metadata, like updatedAt they could only read documents that haven't been updated since x)?

In other words. if I use onSnapshot() listener, I will make x documents read initially, then 1 document each time a document changes. My question is: If I close the app and a document changes, then when I open the app, is 1 read made or x + 1?

It is important for me because I have a bunch of initial calls and I'm wondering how that'd affect the cost($).

It's also important to know for data modeling and how it affects the cost.

Every time you perform a new query against the server (this is the default), it will cost a read, and the documents will have to be transferred. It will not use the cache unless there is no connection, or your specifically target the cache for the query. Quitting and returning to the app doesn't change this behavior at all.

I suggest reading this: https://medium.com/firebase-developers/firestore-clients-to-cache-or-not-to-cache-or-both-8f66a239c329

It depends on the type of listener

OnChange() will read only when data changes

addListenerForSingleValueEvent will check just once, and if it is the onCreate section, it will be executed immediately

addValueEventListener will keep checking constantly, but will log as a read only if the data changes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM