quickstart-android: Firebase database Persistence not working

I m not able to enable the presistence, see here i m doing this as per the new api

` public class App extends Application { @Override public void onCreate() { super.onCreate(); FirebaseDatabase.getInstance().setPersistenceEnabled(true); }

} `

but still its not working, its crashing and giving error:

FATAL EXCEPTION: main Process: com.vrjco.v.someapp:background_crash, PID: 30170 java.lang.RuntimeException: Unable to create application com.vrjco.v.someapp.App: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn’t exist. at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4752) at android.app.ActivityThread.access$1600(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5653) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn’t exist. at com.google.firebase.FirebaseApp.getInstance(Unknown Source) at com.google.firebase.FirebaseApp.getInstance(Unknown Source) at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source) at com.vrjco.v.someapp.App.onCreate(App.java:14)

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (3 by maintainers)

Most upvoted comments

@vrjgamer FirebaseApp is initialized by a ContentProvider so it is not initialized at the time Application.onCreate is called.

This means you have two options:

  1. Manually initialize FirebaseApp in your Application.onCreate (not recommended)
  2. Call setPersistenceEnabled some other time in your application, like when the first Activity launches.

Be careful though, you can only call setPersistenceEnabled before you have called getReference() for the first time. For this reason I often wrap FirebaseDatabase in a singleton like this:

public class MyDatabaseUtil {

  private static FirebaseDatabase mDatabase;

  public static getDatabase() {
    if (mDatabase == null) {
      mDatabase = FIrebaseDatabase.getInstance()
      mDatabase.setPersistenceEnabled(true);
      // ...
    }

    return mDatabase;

  }

}

And then I call MyDatabaseUtil.getDatabase() from my Activities when I want to use the Database.

@csbenz it will not work because FirebaseApp is not automatically initialized until after your Application onCreate is called.

Also there are a few reasons in general why you may want to avoid subclassing Application if you can:

  • If you use Firebase Crash Reporting, your Application’s onCreate may be called multiple times due to the multi-process nature of that service.
  • If your app uses multidex there can be some issues, see MultidexApplication
public class DatabaseUtil {

    private static FirebaseDatabase mDatabase;

    public static FirebaseDatabase getDatabase() {
        if (mDatabase == null) {
            mDatabase = FirebaseDatabase.getInstance();
            mDatabase.setPersistenceEnabled(true);
        }

        return mDatabase;
    }
}

For Kotlin Try this:

class DatabaseUtil {
    companion object {
        private val firebaseDatabase: FirebaseDatabase = FirebaseDatabase.getInstance()

        init {
            firebaseDatabase.setPersistenceEnabled(true)
        }

        fun getDatabase() : FirebaseDatabase {
            return firebaseDatabase
        }
    }
}

Why not just call FirebaseDatabase.getInstance().setPersistenceEnabled(true); in the onCreate method of the first activity you will ever use before you do anything? I just added this to my code and I haven’t run into any problems, granted I haven’t gone through very extensive testing, but I can login, logout, restart the app while logged in, and perform various database related activities without crashing online or off.