Agung
Agung

Reputation: 13803

Do I read all documents again if there is a change in the Firestore database?

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

Upvotes: 2

Views: 4075

Answers (2)

Sam Stern
Sam Stern

Reputation: 25134

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.

Upvotes: 4

Yatima
Yatima

Reputation: 168

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

Upvotes: 7

Related Questions