Com.Google.Firebase.Database.Databaseexception: Calls to Setpersistenceenabled() Must Be Made Before Any Other Usage of Firebasedatabase Instance

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

Something like this (iirc):

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

Android Firebase - setPersistenceEnabled(true) crashing the app

This part mFirebaseInstance.setPersistenceEnabled(true); should be just in first Activity. It's shouldn't be called more than once.

Better solution would be to put that line in onCreate method of your Application class. You can read more about it here.

Persisted Firebase Database crashes on Orientation change

Because you need tell your widgets just stay as it is when orientation changed. So, you need to put one line in your <activity> in AndroidManifest.xml file:

<activity
android:name="NAME_OF_YOUR_ACTIVITY"
android:configChanges="keyboardHidden|orientation|screenSize" />

How to get an error message when data could not be written into the Firebase Database using Android

Why sometimes the data can be written into the Firebase Database and sometimes not and why this seems to happen "randomly"? Can you think of some reasons?

Most likely this it's because of the lack of internet connection. To solve this, you should enable offline persistence. In Java code, this can be done using the following line:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

According to the docs:

Firebase applications work even if your app temporarily loses its network connection.

Firebase apps automatically handle temporary network interruptions. Cached data is available while offline and Firebase resends any writes when network connectivity is restored.

And to answer your second question:

More importantly: How can I get an error message when the data could not be written into the Firebase Database such that I can inform the user? And related to this: Why do I not get a Log.e("dbTAG", task.getException().getMessage()); message when the data was not successfully written into the Firebase Database? So the else branch of the onComplete method is not executed when the data is not successfully stored.

You are handling the errors in a correct way using:

Log.e("dbTAG", task.getException().getMessage());

You'll get an error message, when there is a problem with writing the data, meaning the write operation is rejected by the Firebase severs, for example, when you have improper security rules.

Please also note, that Firebase Realtime Database SDK doesn't throw an Exception when there is no internet connection, and it makes sense since Firebase Realtime Database is designed to work offline. Behind the scenes, Firebase Realtime Database SDK tries to reconnect until the devices regain connectivity. However, it will indeed throw an Exception, when Firebase servers reject the request due to a security rule issue.



Related Topics



Leave a reply



Submit