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

前端 未结 8 917
慢半拍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:39

    Create an application class that will be used across your entire application and initialize firebase Persistence in it:

    FirebaseHandler class You can call/name the class whatever you want to name it

    public class FirebaseHandler extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            FirebaseDatabase.getInstance().setPersistenceEnabled(true);   
        }
    }
    

    Add the application class to your Manifest:

     <application
        android:name=".FirebaseHandler"
        android:allowBackup="true"
        android:icon="@mipmap/app_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
    

    This way data persistance will be applied to your entire project. Inorder to apply disk persistence to specific data.

    DatabaseReference myRef=FirebaseDatabase.getInstance().getReference("people"); 
    myRef.keepSynced(true);
    

    This will keep your offline data synced and up to date

    myRef.keepSynced(true);
    

    Learn more about Disk Persistence Here

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 05:45

    Just add this at the top of your activity class:

    static {
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    }
    

    Worked for me, not sure if it's the best practice though.

    0 讨论(0)
  • 2020-11-29 05:45

    You could try checking if there are any initialized firebase apps before calling setPersistenceEnabled.

    if (FirebaseApp.getApps(this).size() == 0)
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    
    0 讨论(0)
  • 2020-11-29 05:54

    Something like this (iirc):

     if (mDatabase == null) {
         FirebaseDatabase database = FirebaseDatabase.getInstance();
         database.setPersistenceEnabled(true);
         mDatabase = database.getReference();
         // ...
     }
    
    0 讨论(0)
  • 2020-11-29 05:55

    If you are using Kotlin, the following code worked for me. I declared as private member variable at the top of the class.

    private val firebaseInstance = FirebaseDatabase.getInstance().apply { setPersistenceEnabled(true) }
    
    0 讨论(0)
提交回复
热议问题