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)
@vrjgamer
FirebaseAppis initialized by aContentProviderso it is not initialized at the timeApplication.onCreateis called.This means you have two options:
FirebaseAppin yourApplication.onCreate(not recommended)setPersistenceEnabledsome other time in your application, like when the first Activity launches.Be careful though, you can only call
setPersistenceEnabledbefore you have calledgetReference()for the first time. For this reason I often wrapFirebaseDatabasein a singleton like this: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
Applicationif you can:For Kotlin Try this:
Why not just call
FirebaseDatabase.getInstance().setPersistenceEnabled(true);in theonCreatemethod 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.