I have a doubt about offline capability in Firebase android. After enabling Firebase offline capability,
do we get all data of our Firebase app including root data or only logged-in User data as cache?
final FirebaseDatabase database = FirebaseDatabase.getInstance();
if (database != null) {
database.setPersistenceEnabled(true);
database.getReference().keepSynced(true);
}
I am using this code snippet for enabling Offline capability in Application class
Firebase has great option of using their database and sending data to their db even if you are offline, and then when the connection is up again, it sends automatically the data to the db.
By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache.
Cloud Firestore supports offline data persistence. This feature caches a copy of the Cloud Firestore data that your app is actively using, so your app can access the data when the device is offline. You can write, read, listen to, and query the cached data.
Cloud Storage for Firebase stores your files in a Google Cloud Storage bucket, making them accessible through both Firebase and Google Cloud. This allows you the flexibility to upload and download files from mobile clients via the Firebase SDKs for Cloud Storage.
Here, I got answer for my question from Firebase Support Team
Actually, the main reason, I'm getting all data (including logged-in user data and all other user's data) of my firebase app as cache in my device, because I'm using keepSynced() on root node of my database.
Take a look on below code snippet, which I'm using for enabling offline capability:
final FirebaseDatabase database = FirebaseDatabase.getInstance();
if (database != null) {
database.setPersistenceEnabled(true);
database.getReference().keepSynced(true);
}
As in above code snippet, database reference is referencing to root node of database.
Now, on this database reference, I'm using keepSynced() method. So, this is keeping all data in sync as cache in device and this is not a good thing to do.
Ideally, we should use keepSynced() method on those databaseReferences necessary for our app to work offline.
Here in above code snippet, I'm making one more mistake which is I'm using setPersistenceEnabled() on Application class. I should move this on my launcher Activity class.
As per my above code snippet, setPersistenceEnabled() will be called every-time, when app will start first time. This should be used only once after installing the app.
We can call setPersistanceEnabled() like the below code snippet for calling it once.
@Override
public void onCreate() {
super.onCreate();
if (!FirebaseApp.getApps(this).isEmpty()) {
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With