How to reduce the firebase database query again and again?

Viewed 506

I am currently developing an e-commerce app in Flutter which is using Firebase as a backend. I have some concerns and confusion regarding the cloud firestore therefore I need your help.

Some of the problems that I am facing are:

  1. Let's say I have 1000's of products in my collection which I gradually want to load when the user runs and scroll through the app, the problem is each time the user quits and runs the app again, the database is called again for the same data which I have already served and eventually costs me more reads.

I don't know how to deal with this situation and I can't find a proper solution too.

  1. I want a way to load the product once and save it on users' memory so that next time when a user runs the app, at first it tries to load from the cache and after that query, the server for more data and again saves that to the cache.

  2. I just want the app to listen and update the cache only when there are some changes in the database, like if I change the price or maybe title of the product. I don't want to fetch every time user runs the app.

  3. My simple question is whats the efficient way to manage this type of situation in Flutter? Please help me and provide some guidance on this issue.

If there are any videos or blog articles that you think will help me with this situation please kindly mention them as well.

2 Answers

That's what I usually do.

Save an integer variable in the database that indicates the version of the data, for example:

version = 1

Each time you reopen the app, read the version from the database, and if it is different from the version saved locally in the user's device, reload the data from the online database.

To save the data locally in Flutter you can use a database like Hive (very high performance) or Postgres (good to make queries but a little slower)

Also, when scrolling products, if they have not yet been saved offline, add them to the database.

Since there is a lot of data, when you update even one, you will download all the data from the database again. If you don't want to read everything from the database, instead of saving an integer variable, you can save an array with the added or modified product id's, and query only on the latter by emptying the array when you saved the data

Adding to @Alexandru answer you can use Firestore's inbuilt feature to cache and fetch data without increasing your read operations. Set constant field in a document to keep track of data changes that is if the list is updated or not and listen to that and only fetch that updated data and only once. In all other scenarios, the best solution is to use Firestore persistence feature. It will store fetched data and supply that even when offline. So two good things.

Persistence is enabled by default following cloud_firestore:0.8.2+. You can enable it otherwise like this Firestore.instance.settings(persistenceEnabled: true);

When fetching data you can put the source argument to cache which is otherwise set to server. Better

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

But do remember to disable editing firebase data using persistence because all changes you make while offline are stored and sent to Firestore when online. This might mess things up so make sure to keep that flow clear and check internet connection for updating data.

You can read more about source configuration here : https://pub.dev/documentation/cloud_firestore_platform_interface/latest/cloud_firestore_platform_interface/Source-class.html

Related