Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we get all our Firebase data as cache after enabling offline capability in android?

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

like image 795
raghav_si Avatar asked Mar 24 '17 06:03

raghav_si


People also ask

Does Firebase Storage work offline?

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.

How does Firebase offline sync work?

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.

Does Firebase cache data?

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.

Where is Firebase data stored?

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.


1 Answers

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);
  }
}
like image 161
raghav_si Avatar answered Nov 15 '22 05:11

raghav_si