I am developing an app using Firebase as backend.I am trying to implement disk persistence provided by Firebase but my app crashes when i restart the app. The document says to write Firebase.getDefaultConfig().setPersistenceEnabled(true)
to write prior to any firebase reference, I did so but its not working.
Here is portion of my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
firebase.getDefaultConfig().setPersistenceEnabled(true);
firebase = new Firebase(getString(R.string.firebase_url));
setContentView(R.layout.activity_main);
//remaining code
}
Log output:
Caused by: com.firebase.client.FirebaseException: Modifications to Config objects must occur before they are in use
at com.firebase.client.core.Context.assertUnfrozen(Context.java:124)
at com.firebase.client.Config.setPersistenceEnabled(Config.java:155)
at activity.MainActivity.onCreate(MainActivity.java:148)
at android.app.Activity.performCreate(Activity.java:5984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
Got it! The problem was Firebase.setAndroidContext(this);
and Firebase.getDefaultConfig().setPersistenceEnabled(true);
are to be declared only once per application, as the documentation says (https://www.firebase.com/docs/android/api/).
So, the above mentioned two statements are to be included in the onCreate() of application java file and not inside activity java file. Also they should be included before creating any firebase object. That solves the problem :).
I was also facing the similar problem and finally solved the problem, just check if the isPersistenceEnabled
is false and then only set the persistence. Do this on your onCreate
method
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
if (Firebase.getDefaultConfig().isPersistenceEnabled() == false)
Firebase.getDefaultConfig().setPersistenceEnabled(true);
Firebase.setAndroidContext(this);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With