Asym
Asym

Reputation: 1938

Android Firebase - setPersistenceEnabled(true) crashing the app

So I was just trying out Firebase to update Toolbar label of activities from realtime database. I am getting the following error:

12-21 00:25:19.890 10295-10295/com.xlr8labs.FirebaseAuth E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.xlr8labs.FirebaseAuth, PID: 10295
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xlr8labs.FirebaseAuth/com.xlr8labs.FirebaseAuth.RegisterActivity}: 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:2341)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2393)
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                           at android.os.Looper.loop(Looper.java:193)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5295)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
                                                                           at dalvik.system.NativeStart.main(Native Method)
                                                                        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.zzhM(Unknown Source)
                                                                           at com.google.firebase.database.FirebaseDatabase.setPersistenceEnabled(Unknown Source)
                                                                           at com.xlr8labs.FirebaseAuth.RegisterActivity.onCreate(RegisterActivity.java:35)
                                                                           at android.app.Activity.performCreate(Activity.java:5264)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2393) 
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                           at android.os.Looper.loop(Looper.java:193) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5295) 
                                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
                                                                           at dalvik.system.NativeStart.main(Native Method) 

Even though I'm using setPersistenceEnabled() before calling any other methods on my database instance, I'm still getting this error. Here is my Activity.java code:

public class RegisterActivity extends AppCompatActivity {
 private FirebaseDatabase mFirebaseInstance;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        // ------------------ Update activity label from database in realtime ------
        mFirebaseInstance = FirebaseDatabase.getInstance();
        mFirebaseInstance.setPersistenceEnabled(true);
        mFirebaseInstance.getReference("RegisterLabel").keepSynced(true);
        mFirebaseInstance.getReference("RegisterLabel").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.e(LOG_TAG,"Register label changed");
                String appTitle = dataSnapshot.getValue().toString();
                getSupportActionBar().setTitle(appTitle);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        // ---------------------------------------------------------------------------

What is causing the crash. I'm using the exact same snippet on two other activities...

Upvotes: 2

Views: 4696

Answers (5)

Meet Patel
Meet Patel

Reputation: 1

You just simply uninstall the previously installed app. And let it install the fresh app. It will resolve the problem.

Upvotes: 0

Himanshu Rawat
Himanshu Rawat

Reputation: 677

You can use a Model class and use Method inside it such as the one mentioned below.

public class DatabaseUtility {

    private static FirebaseDatabase database;

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

This will return database instance and you can use database.getReference(); method to get Root reference. This will also save some time if the database object is already present.

Upvotes: 0

Rakib Joarder
Rakib Joarder

Reputation: 41

boolean flag = true; //before onCreate() method

//inside onStart() Method
if(flag)
{ 
  firebaseDatabase.setPersistenceEnabled(true);
  flag = false;
}

Upvotes: 0

Vladimir Jovanović
Vladimir Jovanović

Reputation: 2267

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.

Upvotes: 6

Amit Upadhyay
Amit Upadhyay

Reputation: 7391

Remove the line mFirebaseInstance.setPersistenceEnabled(true); from your RegisterActivity class.

Make a custom class which should extend Application and write it there.

Example :

public class JustExample extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        if (!FirebaseApp.getApps(this).isEmpty())
        {
            FirebaseDatabase.getInstance().setPersistenceEnabled(true);
        }
    }
}

Upvotes: 0

Related Questions