user6617128
user6617128

Reputation:

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

Upvotes: 6

Views: 998

Answers (2)

user6617128
user6617128

Reputation:

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);
  }
}

Upvotes: 3

Oussema Aroua
Oussema Aroua

Reputation: 5329

read this

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.

link

Upvotes: 0

Related Questions