Firebase Realtime Data - "setPersistenceEnabled() must be made before any other usage"

    Just a small issue I encountered with my android app with how to correctly implement firebase real time data to be persistent. I did found a few notices on how to correct this and most of them was just making the database instance static or with a flag to see if the object wasn't already created. A better solution would be having the later version of the object handle this in a method. What I did was just wrap it with my own object and create a method like its explained in the github issue. The purpose of this post is really just make the information more available and public so the documents get updated or the code get addressed.

Code
public class FirebaseUtil {

    private static FirebaseDatabase mDatabase;

    public static FirebaseDatabase getDatabase() {
        if (mDatabase == null) {
            mDatabase = FirebaseDatabase.getInstance();
            mDatabase.setPersistenceEnabled(true);
        }

        return mDatabase;
    }
}


Usage
DatabaseReference myRef = FirebaseUtil.getDatabase().getReference();


Resources
Enabling Offline Capabilities on Android
Firebase database Persistence not working · Issue #15 · firebase/quickstart-android
firebase - setPersistenceEnabled(true) crashes app - Stack Overflow

Popular posts from this blog

Aspetcore: NLog with Postgresql

Mongodb Cheatsheet

Installing Windows on Acer chromebook 15 cb3-532