Firestore: How to cache writes+changes and upload only at the end on mobile client

Viewed 88

Please, I have a project in which a user needs to configure an object(e.g his shop). So I want the user to be able to read from Firestore during the modification but not to write to the online database directly. Instead, I wish he/she should write or modify to his offline cache and when he taps the "save" button, all changes should be committed online.

Thanks

1 Answers

I wish he/she should write or modify to his offline cache and when he taps the "save" button, all changes should be committed online.

There is no way you can tell Firestore to write the data to cache. This is happening by default when the user has no internet connection. According to the docs, Firestore has offline persistence enabled by default:

For Android and iOS, offline persistence is enabled by default.

However, you can tell Firestore to read the data only from the cache by specifying the source.

So when the user finishes configuring the shop, there are two cases. The first one is when the user has internet connectivity, meaning that all data is saved on the Firebase server, or when the user has no internet connectivity and the data is written to the cache. Once the user regains connectivity, all data is sent to the Cloud.

If you want to save multiple operations when configuring a shop, then you might also consider using SharedPreferences, which is available across your entire application.

Related