I have set up a realtime Firebase-Database and it communicates without a glitch with the android app I am building … until I add db.setPersistenceEnabled(true);
(db
being the instantiated global FirebaseDatabase variable) and do an orientation change.
Researching on StackOverflow and the documentation I saw a few cases where this was solved by using an if-clause to ensure the code does not retry to persist. But even with that in place, the app crashes.
I am suspecting that the ValueEventListener is somehow playing a role in this. I have tried to add and remove the listener in onStart() and onStop() but that too did not prevent the crash.
Here is my current version of the code (having removed all the above-mentioned trials so as not to add confusion):
private FirebaseDatabase db;
private DatabaseReference dbRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
db = FirebaseDatabase.getInstance();
db.setPersistenceEnabled(true);
isPersistenceEnabled = true;
dbRef = db.getReference("meditations");
dbRef.keepSynced(true);
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
meditations.clear();
List<String> keys = new ArrayList<>();
for (DataSnapshot keyNodes : dataSnapshot.getChildren()) {
keys.add(keyNodes.getKey());
Meditation meditation = keyNodes.getValue(Meditation.class);
meditations.add(meditation);
Log.v(getClass().getSimpleName(), "medtiation added");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Does anyone have experience with this scenario? Am I overlooking something?
ETA: Adding stacktrace for the full picture (in case others have a similar problem):
2
019-05-14 15:29:31.175 16705-16705/com.example.android.meditationhub E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.meditationhub, PID: 16705
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.meditationhub/com.example.android.meditationhub.ui.MainActivity}: com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4621)
at android.app.ActivityThread.-wrap19(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.
at com.google.firebase.database.FirebaseDatabase.assertUnfrozen(com.google.firebase:firebase-database@@17.0.0:316)
at com.google.firebase.database.FirebaseDatabase.setPersistenceEnabled(com.google.firebase:firebase-database@@17.0.0:284)
at com.example.android.meditationhub.ui.MainActivity.onCreate(MainActivity.java:46)
at android.app.Activity.performCreate(Activity.java:6725)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4621)
at android.app.ActivityThread.-wrap19(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
ETA2: I see this has been marked as a duplicate question. I had gone over the solutions provided for the question linked and none worked in my case (as I stated above).