Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase disk persistence error : Modifications to Config objects must occur before they are in use

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)
like image 815
Vivek Jyoti Avatar asked Dec 08 '22 00:12

Vivek Jyoti


2 Answers

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 :).

like image 117
Vivek Jyoti Avatar answered Jan 12 '23 00:01

Vivek Jyoti


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);

}

like image 38
Surendra Shrestha Avatar answered Jan 11 '23 23:01

Surendra Shrestha