flutroid
flutroid

Reputation: 1396

Questions about firestore cache and reads charge

There are my understand how firestore cache and read charge will behave in those scenarios.And I am not sure it's right or not.

If I have 2000 documents in a collection.this collection name "testCollection".those documents won't change , update or delete. My client is android with already enable offline persistence and device is currently online

1.

db.collection("testCollection").onSnapshot(function(doc) {});

2000 reads charged and those document are cached. then reopen app and run same code again

db.collection("testCollection").onSnapshot(function(doc) {});

another 2000 reads charged cause firestore need to check each document is up to date or not. So 2000+2000 reads charged

2.

I am not sure how this behave. just run the same code together

db.collection("testCollection").onSnapshot(function(doc) {}); 
db.collection("testCollection").onSnapshot(function(doc) {});

I think is 2000 reads charged because data is keeping up to date

3.

db.collection("testCollection").limit(300).onSnapshot(function(doc) {}); 
db.collection("testCollection").limit(800).onSnapshot(function(doc) {}); 

Total 1100 reads charged.cause it's different query.

Do I have something misunderstand or something wrong ?

Upvotes: 10

Views: 1887

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 1

2000 reads charged and those documents are cached.

That's correct since it's the first time when you perform those read operations.

another 2000 reads charged cause Firestore needs to check each document is up to date or not. So 2000+2000 reads charged

Once the documents are in your cache and those documents aren't changed (as you say), all read operations are coming from the cache. You won't be charged for read operations that are coming from the cache.

I think is 2000 reads charged because data is keeping up to date

You'll be charged with other read operations only if the data on the Firebase servers is changed, otherwise, you'll get the data from the cache.

Total 1100 reads charged.cause it's a different query

If you have already performed the initial query and you already got those 2000 documents, if you perform another query no matter if you are using a limit or not, you read all those documents from the cache.

Upvotes: 11

Related Questions