3

I have been using and testing my Android app thoroughly for the last 2 weeks.

I am using the Firebase Realtime Database exhaustively in my app and the most important thing is that, my app needs to work offline seamlessly.

I have a splash screen, where I sync all the data for a particular user node just while starting and proceed.

I using keepSynced(true); and setPersistenceEnabled(true); for sure.

Have noticed a serious issue, which is causing a lot of problem.

Scenario 1

I run the app for the first time with internet connection, the app works fine.

Scenario 2

I run the app for the first time without internet connection, nothing works. No callback is fired.

Scenario 3

I run the app consecutively with/without internet connection, it works fine.

Scenario 4

But, when I run the app after 1 or 2 days without internet connection, nothing works. No callback is fired.

I seems that the disk persistence is cleared automatically, that is why the data is not available offline and no callback is fired.

So, how can I control when the disk persistence is cleared? Is there a way to make sure that the persistence is never cleared? Is there a way to configure the size of the disk persistence also?

EDIT

This is what I am doing in the onCreate() of my application class.

private void initializeFirebaseDatabase() {
        if (!FirebaseApp.getApps(this).isEmpty()) {
            FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
            firebaseDatabase.setPersistenceEnabled(true);
        }
}
Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
  • Hi Artitra, were you able to solve your issue? I'me seeing similar issues in 'com.google.firebase:firebase-database:9.2.0'. Since it is hard to reproduce the issues, I'm unsure if updating to the latest version solves it... – Niels Oct 06 '16 at 09:05
  • I was unable to reproduce the problem thereafter. – Aritra Roy Oct 08 '16 at 14:24

2 Answers2

0

Check this link out com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance

I guess this question is similar to the one in the link and its solved.

Community
  • 1
  • 1
Siddhesh Dighe
  • 2,894
  • 3
  • 16
  • 16
  • 1
    I am doing this my application class before any other instance is created. So this answer doesn't help at all. – Aritra Roy Jun 18 '16 at 19:08
  • do you have a separate class which extends android.app.Application, with FirebaseDatabase.getInstance().setPersistenceEnabled(true); in its onCreate ? and if yes have you linked that Class to your Application with android:name="com.example.MyFirebaseApp" ? – Siddhesh Dighe Jun 18 '16 at 19:16
  • Yes for sure. And Firebase also doesn't allow setPersistence() method to be called after any database initialization. Its throws an exception. – Aritra Roy Jun 18 '16 at 19:18
  • can you add the screenshots or the code of how you implemented setPersistence() ? it might help solve the issue – Siddhesh Dighe Jun 18 '16 at 19:21
  • Do you call initializeFirebaseDatabase Method in all the Activities where you use Firebase References? – Siddhesh Dighe Jun 19 '16 at 09:01
  • I had mentioned, that this is the onCreate() method of my application class. – Aritra Roy Jun 19 '16 at 09:02
0

I had the same problem, with a Splash screen setting setPersistenceEnabled(true) there, but when I called finish() and tried to open the app again got the error. Solved it killing the app instead of Calling finish().

I use OnBackPressed() to finish the activity and a Boolean isFinishing flag, to distinguish between sending the app to background and killing it.

And finally I kill it in OnDestroy() with:

android.os.Process.killProcess(android.os.Process.myPid());
Guanaco Devs
  • 1,822
  • 2
  • 21
  • 38