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

前端 未结 8 950
慢半拍i
慢半拍i 2020-11-29 05:15

I am having a problem when I try to setPersistence in fIREBASE,can someone please explain on how to go about it,

protected void onCreate(Bundle savedInstance         


        
8条回答
  •  被撕碎了的回忆
    2020-11-29 05:45

    According to Firebase Documentations setPersistenceEnabled is to be called only once (before any other instances of FirebaseDatabase are made)

    So the solution to this issue for me was the following

    1. You need to create a class which extends android.app.Application and setPersistenceEnabled(true) over there.

    For Example

    class MyFirebaseApp extends android.app.Application 
    
    @Override
    public void onCreate() {
        super.onCreate();
        /* Enable disk persistence  */
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    }
    
    1. In the Manifest, link the MyFirebaseApp class to the application tag

    For Example

    in your application tag add the following

    android:name="com.example.MyFirebaseApp"
    

    this should work fine.

    Also don't use setPersistenceEnabled in any other Activity.

提交回复
热议问题