Disk persistence with Fireblade, avoid calling it twice

2

I do not think it is necessary to show them code to help me with the following:

I have a login with firebase, and I use disk persistence for when I do not have an internet connection and so I can continue using my app, once I log in, the user remains active even if I close the application, because when I'll open the application again without needing to register again, that's fine, the problem is that if I post:

 "FirebaseDatabase.getInstance().setPersistenceEnabled(true);" 

where I verify the login, each time you close the app and enter will verify the login and will call each time:

"FirebaseDatabase.getInstance().setPersistenceEnabled(true);" 

which gives an error since it can only be called once.

How could I solve it?

    
asked by Wayner 20.02.2017 в 02:26
source

1 answer

1

There are several options to solve this, obviously the problem arises when you already have an instance of the database, you can check if the instance is different from null in this way get the instance and enable persistence:

private static FirebaseDatabase fbDB;

    @Override
        protected void onCreate(Bundle savedInstanceState) {

            if(fbDB == null) {
                fbDB = FirebaseDatabase.getInstance();
                fbDB.setPersistenceEnabled(true);
            }
         ...
         ...
        }

Another option to avoid deleting the application is to add the same within onResume () to get the instance of FirebaseDatabase when returning from background () or background:

@Override
public void onResume(){
    super.onResume();
     if(fbDB == null) {
                    fbDB = FirebaseDatabase.getInstance();
                    fbDB.setPersistenceEnabled(true);
     }
}
    
answered by 20.02.2017 в 02:40