英文:
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("<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();
}
答案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(<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论