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
Android: How to Change the Actionbar "Home" Icon to Be Something Other Than the App Icon
Generate Barcode Image in Android Application
Filtering Accelerometer Data Noise
Asynctask: Where Does the Return Value of Doinbackground() Go
Change the Right Margin of a View Programmatically
Android Retrofit - Onprogressupdate for Showing Progress Notification
Cannot Lower Case Button Text in Android Studio
Android: How to Convert Whole Imageview to Bitmap
Libgdx Spritebatch Render to Texture
How to Create Converter for My Class in Android Retrofit Library
Login Error: There Is an Error in Logging You into This Application. Please Try Again Later
Viewpager Intercepts All X-Axis Ontouch Events. How to Disable
How to Upload an Image in Parse Server Using Parse API in Android
Using JSON File in Android App Resources
Mylib.So Has Text Relocations. This Is Wasting Memory and Is a Security Risk. Please Fix