2
votes

let say I try to get a list of eventID that liked by the user by using Firestore listener. I also use offline persistence.

from this thread : Firestore - Using cache until online content updates

I know that the listener will be fired immediately with the results from the cache and if there is a change result, I will get another snapshot with the changes.

enter image description here

in swift, the reference to get the list is:

FirestoreDocumentReference.users(uidUser: uid).reference().collection("likedEvents").addSnapshotListener({ (snapshot, error) in )}

let say at first in the firestore database I have 100 eventID that liked by the user, and then the user like another 50 event, it means I have 150 liked eventID in the database now.

since there is a change in the database from 100 to 150, Firestore will check with the server right? and it will give me another snapshot.

my question is ....

Do I read 150 documents or just 50 documents after receiving new snapshot from firestore? I mean, Do I read all documents again or just changed document?

because read operation will affect the pricing of firestore

2
It depends on how you read the events. Since you don't show that, it is hard to answer. In general the Firestore client and server work together to minimize the number of documents that are read.Frank van Puffelen
I use this code to read the events: FirestoreDocumentReference.users(uidUser: uid).reference().collection("likedEvents").addSnapshotListener({ (snapshot, error) in )}sarah
sir, could you please help me elaborate a little bit about "how you read the event" ?sarah
A snapshot listener will give you a snapshot with all documents that are in range of the query. If those documents are up-to-date in the local cache, the listener will return the document from the cache without reading it from the server. The document is up-to-date in the cache, if the cache is enabled (it is by default), the document has been loaded before, and it hasn't been modified. So if all is well, the second time your snapshot listener callback gets called counts only for the additional 50 documents. See stackoverflow.com/a/48491561Frank van Puffelen
I believe this video will help you.Rosário Pereira Fernandes

2 Answers

7
votes

Just 50 :-)
https://firebase.google.com/docs/firestore/pricing#listens

you are charged for a read each time a document in the result set is added or updated

4
votes

The answer here is it depends.

When open a new snapshot listener with Firestore you get something called a "resume token" which you can think of like a session. A resume token helps the server try and send you only the documents that have changed since the last snapshot you received. However these tokens expire in <= 30 minutes. So it depends on how long the 50 new documents are added after you initialize your snapshot listener.