Control when Firebase Database syncs data - firebase

I'm thinking moving an Android app's persistent data to Firebase Database. Currently I use Sqlite with a Python HTTP REST service.
In the app, I have a big list of cities, called citiesList. Since the list is quite big and hardly updated, I don't want the app fetch the list whenever it goes online.
My current strategy is provide a citiesListVersion. When the app goes online, it checks citiesListVersion. If the server's citiesListVersion is newer, the app will fetch citiesList from server. Otherwise, the app continues working with the cached data.
My question is: can I keep my current strategy when moving to Firebase Database? As I understand, Firebase tries to sync data whenever the app goes online.

Related

How to use firebase realtime-database in offline mode in Flutter app?

I came across a wonderful feature of Firebase offline feature. I integrated that in my app just by writing one line of code in my main.dart file after initializing Firebase await FirebaseDatabase.instance.setPersistenceEnabled(true);
Question 1 :
I couldn't able to understand the database.keepSynced(true) function because without using this line of code, my app is persisting old as well as fetching new updated data, so what this exactly does ?
Question 2 :
How could I prevent the write operations when a user is offline, because I read that after setting persistence enabled, it makes a queues of write operations and update them when user gets online, so how could I stop this ?
Question 3 :
Is this persistence feature going to work in IOS device as well or need some permission settings first ?
Thanks
When you call FirebaseDatabase.instance.setPersistenceEnabled(true) you're allowing Firebase to create a local file on the device where it persists any data it's recently read, and all writes that are pending while the device is offline.
When you call keepSynced(true) on a node, you are telling the SDK to always keep that node synchronized. It essentially creates a onValue listener on the node without any handler code, so you're purely doing this to keep the data synchronized for when the device does go offline.
By combining keepSynced(true) with setPersistenceEnabled(true), you're specifying that you want the app to continue working when it's offline across restarts, and which data is needed for that.
If you call keepSynced(true) on the root of your database, you're telling the SDK to synchronize all data in the database to the disk cache. While this may initially be a quick way to get offline mode for your app working, it typically won't scale when you more people start using your app.
If you only want to allow write operations while the client has a connection to the database backend, you can register a local listener to the .info/connected node, which is a true value when there is a connection and false otherwise.
Note that Firebase doesn't require this, as it queues the pending writes and executes them when the connection is restored. In general, I'd recommend working with the system here instead of against it, and also trying to make your app work gracefully in the offline scenario. In many cases there is no need to disable functionality while the app is offline.
Offline disk persistence is available on Android and iOS, but not on web.

Under which circumstances does Firebase Firestore database get downloaded?

I'm creating an android app which uses Firebase Firestore database to store data. I store about 3600 questions and about 400 images. I know that firestore support offline. But I'm not sure whether the database download itself or not every time I request data from it. And under which circumstance does the firestore database get downloaded from the cloud? Does the database get only updated when the cloud database change?
Every query performed while the app is online will download all the necessary data from the database to satisfy that query.
Any data cached locally as a result of a prior query is only used when the app is offline. As suggested by the documentation, the idea is for your app to be usable when internet connectivity is interrupted. The assumption is that the connectivity will eventually come back, and queries will revert to using the online database as the primary source of data.

Firestore cache data before restarting app

I have no code but only a special question regarding Cloud Firestorage by Google.
Im using currently a listener for my Firestorage within my react-native-app which I also use expo for.
I know that as long as I stay in the app and tab out of the window, all data downloaded by the listener is getting stored in a temporary cache so when I come back to the screen with the listener, the listener doesnt need to download all data again but rather the missing sample.
My question would be: Does this also apply if I close the app entirely, for example for multiple days so that my token for authentification expires and I need to log myself in again into the app or rather into my Firebase?
(I want to prevent downloading all data the listener points to only because I restarted the app.)
As explained here the firestore persistance for the web is disk persistance so it survies App and devices restarts. Multiple days should also be fine.
I have a Web App tha is using firestore and very heavily offline capabilities. We never had issues with that.

Flutter: Minimising Firestore read/write operations

I am new to Flutter and mobile development in general and currently trying to wrap my head around certain database principles. I am building an note-taking app which uses Firestore for storing note data to the cloud. Users first need to register an account and log in, after which they are able to view and store notes to the cloud. The way I initially designed this was by using StreamProvider connected to the Firestore instance to update the user's list of notes after they add a note to their list.
After some reflection and worries that my app would be 'read' intensive, I realised that what I am trying to build does not require the notes to be constantly fetched from the server, as the data is private to them and should only be fetched by the user that creates it. My solution is instead of creating notes and listening to changes on the Firestore server to update the list, when a user creates a new note, a function should run and update the Firestore server as well as a local nosql database, removing the need for a read operation on the server after writing to it. This would allow me to have a local duplicate copy of the Firestore server and to periodically update it with write operations when needed. The only read operation would be on the app's startup to fetch a copy of the Firestore server.
My questions are:
Is storing a local version of the Firestore server a viable solution to minimise the number of read operations in my app, or am I overcomplicating things?
The StreamProvider allowed me to easily access data relating to the user, notes, etc throughout the app using Provider.of<Model>(Context). How can I easily access this data regardless of where I am in the app?
Given that my Firestore database structure for accessing notes is users/{user.id}/notes/{note.id}/note , how can I get a snapshot of the current logged in user ID and all the child fields in a single request (i.e. all the user's notes in users/{user.id}/notes and user data under users/{user.id}/)

Firebase check for new data on app load

Does firebase have a native way to check if the app has the latest data on the second load?
Here is the app workflow.
When the app loads I get data from firebase and display it to the
user.
Data is also stored locally to be displayed later.
The user closes the app and the reopens it sometime in the future.
How do I check if Firebase had new data and get it only if it has?
What I am doing is that I am storing a TimeStamp on my device and on the database. On each app load I check if the TimeStamp matches.
If it does then I display old local data otherwise make call to get latest.
Is this the correct way?
Basically what I am asking is I want to display locally stored data, but if firebase has new data then I want to get it and display that.

Resources