0

I have an app that has 50k - 60k document reads a day and I can't afford another plan now, so I'm looking for a way to optimize reads, getting less reads possible I can.

The database has 4 collections with some subcollections, I think I might have around 1000 documents in the whole server.

It is there anyway: I can read documents from cloud Firestore (first time the user opens the app) and store on the Firestore cache, after that, make the app only load data from Firestore cache, and if there is a new document(s), it reads these documents only, store to the cache, and keep reading from the cache?

The app could only load data from cache (using the parameter "source") and never directly from Firestore, if possible.

Firestore.instance.collection("images").getDocuments(source: Source.cache);

1 Answer 1

1

Is is there anyway: I can read documents from cloud Firestore (first time the user opens the app) and store on the Firestore cache

That's the default behavior. According to the official documentation regarding Firestore offline persistence:

  • For Android and iOS, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false.

  • For the web, offline persistence is disabled by default. To enable persistence, call the enablePersistence method. Cloud Firestore's cache isn't automatically cleared between sessions. Consequently, if your web app handles sensitive information, make sure to ask the user if they're on a trusted device before enabling persistence.

So there is nothing special that you need to do. Once you open a stream on one of your collections, the data is added to the cache.

after that, make the app only load data from Firestore cache, and if there is a new document(s), it reads these documents only, stores to the cache, and keeps reading from the cache?

That's again the default behavior, but this work as long as the documents in your database are not changed. If a document in the database is changed, you'll be charged with a document read for each document. The mechanism is the same.

Besides that, you can tell Firestore to read data only from the cache if needed, as explained in the answer from the following post:

Sign up to request clarification or add additional context in comments.

14 Comments

But does it work in the same way for StreamBuilder(snapshots) and FutureBuilder(getDocuments) about updating only new documents?
Yes, that's the same mechanism.
I asked because looks like we cannot use parameter "cache" when using StreamBuilder, ".snapshot()", only FutureBuilder(.getDocuments(source: ?)) has.
I was referring to the billing part. Yes, in the case of StreamBuilder there is no way you can use parameter "cache". When you listen for realtime updates, you are listening for documents that are changed on the server. Once you get some updates, all documents are then cached on user's device.
If you are offline a local copy will be created and any database write that is done while you are offline will be synced when you are online again. But then data will still be retrieved from the server
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.