多个 Firebase 项目在一个应用中使用不同的持久性设置

huangapple go评论83阅读模式
英文:

Multiple Firebase Projects in one app with different persistence setup

问题

我想在一个应用程序中设置两个 Firebase,但要使用不同的持久性设置。

一个是启用的,第二个是禁用的。

请告诉我如何进行设置。

我从以下链接中获取了代码,

https://stackoverflow.com/questions/39632243/multiple-firebase-projects-in-one-app

但它没有解释如何为第二个 Firebase 设置持久性。

我想为第一个 Firebase 启用持久性。

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

并且禁用第二个 Firebase 的持久性。

FirebaseDatabase.getInstance().setPersistenceEnabled(false);

正如我们所看到的,getInstance 是一个静态方法,

我们如何知道,

返回的 FirebaseDatabase 实例,

将属于第一个还是第二个 Firebase。

private void initSecondFirebaseAcct()
{
    FirebaseOptions options = new FirebaseOptions.Builder()
        .setApplicationId("<你的应用程序 ID>")
        .setApiKey("<你的 API 密钥>")
        .setDatabaseUrl("<以 'firebaseio.com/' 结尾的数据库 URL>")
        .build();

    try
    {
       FirebaseApp.initializeApp(this, options, "<数据库标识>");
    }catch (Exception e){
       Log.d("Firebase error", "应用程序已存在");
    }

    mMySecondApp = FirebaseApp.getInstance("<数据库标识>");
    mSecondDBRef = FirebaseDatabase.getInstance(mMySecondApp).getReference();
}
英文:

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,

https://stackoverflow.com/questions/39632243/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(&quot;&lt;your application id&gt;&quot;)
        .setApiKey(&quot;&lt;your api key&gt;&quot;)
        .setDatabaseUrl(&quot;&lt;your DB url that ends in &#39;firebaseio.com/&#39; &quot;)
        .build();

    try
    {
       FirebaseApp.initializeApp(this, options, &quot;&lt;database tag&gt;&quot;);
    }catch (Exception e){
       Log.d(&quot;Firebase error&quot;, &quot;App already exists&quot;);
    }

    mMySecondApp = FirebaseApp.getInstance(&quot;&lt;database tag&gt;&quot;);
    mSecondDBRef = FirebaseDatabase.getInstance(mMySecondApp).getReference();
}

答案1

得分: 0

确保您在引用第一个或第二个Firebase应用程序时,需要将特定的FirebaseApp实例作为参数提供给:

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

如果您使用FirebaseDatabase.getInstance(),您将获得默认实例,即第一个数据库应用程序。因此,只需将第二个实例作为参数提供,您就可以继续进行。

英文:

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(&lt;app instance here&gt;).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.

huangapple
  • 本文由 发表于 2020年8月31日 20:29:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63670854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定