question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Firebase database Persistence not working

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
samtsterncommented, Aug 21, 2017

@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.

5reactions
samtsterncommented, Jun 2, 2016

@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
Read more comments on GitHub >

github_iconTop Results From Across the Web

Access data offline | Firestore - Firebase
With offline persistence enabled, the Cloud Firestore client library automatically ... Cloud Firestore's cache isn't automatically cleared between sessions.
Read more >
Flutter Firebase Database persistence not working
Don't use FirebaseInstance to toggle persistent storage. Use FirebaseDatabase object to hold the instance and then set it to enable.
Read more >
14 Realtime Database Offline Capabilities Written by Dean ...
To enable disk persistence, you only need one line of code. Open WhatsUpApplication class and enable persistence on the FirebaseDatabase instance in the ......
Read more >
Realtime Database - React Native Firebase
Currently the examples have only demonstrated working with known reference node keys (e.g. /users/123 ). In some cases, you may not have a...
Read more >
Create a Never Offline Web App!. Overcome PWAs limits and ...
Now we can read, edit or delete any cached document, even without a network connection. If the cache does not contain the requested...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found