Vincent Macharia Vincent Macharia - 4 months ago 229
Android Question

com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance

I am having a problem when I try to setPersistence in fIREBASE,can someone please explain on how to go about it,

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meal_details);

if (mDatabase == null) {
mDatabase = FirebaseDatabase.getInstance().getReference();
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
// ...
}


// FirebaseDatabase.getInstance().setPersistenceEnabled(true);
mDatabase = FirebaseDatabase.getInstance().getReference();

Answer

Something like this (iirc):

 if (mDatabase == null) {
     FirebaseDatabase database = FirebaseDatabase.getInstance();
     database.setPersistenceEnabled(true);
     mDatabase = database.getReference();
     // ...
 }