1

I want to setup 2 Firebase in 1 App, but with difference persistence.

One is enable, and the second is disable.

Please inform me how to set it up.

I take code from link below,

Multiple Firebase projects in one app

but it didn't explain,

how to setup persistence for the second Firebase.

I want to enable persistence for 1st Firebase.

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

And disable persistence for 2nd Firebase.

FirebaseDatabase.getInstance().setPersistenceEnabled(false);

As we see, that getInstance is static method,

how do we know that,

the returned FirebaseDatabase instance,

will belong to 1st or 2nd Firebase.

private void initSecondFirebaseAcct()
{
    FirebaseOptions options = new FirebaseOptions.Builder()
        .setApplicationId("<your application id>")
        .setApiKey("<your api key>")
        .setDatabaseUrl("<your DB url that ends in 'firebaseio.com/' ")
        .build();

    try
    {
       FirebaseApp.initializeApp(this, options, "<database tag>");
    }catch (Exception e){
       Log.d("Firebase error", "App already exists");
    }

    mMySecondApp = FirebaseApp.getInstance("<database tag>");
    mSecondDBRef = FirebaseDatabase.getInstance(mMySecondApp).getReference();
}
Hendra
  • 25
  • 3

1 Answers1

0

The way to make sure you are referencing the first or second Firebase app is to provide the specific FirebaseApp instance as a parameter to:

FireDatabase.getInstance(<app instance here>).setPersistenceEnabled().

If you use FirebaseDatabase.getInstance() you will receive the default instance, the first database app. So just provide the second instance as a parameter and you'll be good to go.

Dharman
  • 30,962
  • 25
  • 85
  • 135
David Velasquez
  • 2,346
  • 1
  • 26
  • 46