gpt4 book ai didi

android - 如何在 android 中删除然后重新初始化 Firebase 应用程序?

转载 作者:行者123 更新时间:2023-12-04 17:39:24 25 4
gpt4 key购买 nike

我尝试处理两个不同 firebase 项目的两个数据库。为此,我必须首先删除 新初始化的应用程序,然后必须重新初始化 应用程序。为此,我尝试使用但每次都失败了。

FirebaseApp.initializeApp(context, firebaseOptions, "secondary").delete();

应该使用什么来删除辅助初始化的 firebase 应用程序?下面是我的代码:

 boolean hasBeenInitialized = false;
List<FirebaseApp> firebaseAppList = FirebaseApp.getApps(Charts.this);

for (FirebaseApp app : firebaseAppList) {

if (app.getName().equals("secondary")) {
hasBeenInitialized = true;

}
}

FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
.setApiKey(apiKey)
.setApplicationId(appId)
.setDatabaseUrl(databaseLink)
.build();

if (!hasBeenInitialized) { //false
firebaseApp = FirebaseApp.initializeApp(Charts.this, firebaseOptions, "secondary");

} else {
firebaseApp = FirebaseApp.getInstance("secondary");

FirebaseApp.initializeApp(Charts.this, firebaseOptions, "secondary").delete();
firebaseApp = FirebaseApp.initializeApp(Charts.this, firebaseOptions, "secondary");

}

secondaryDatabase = FirebaseDatabase.getInstance(firebaseApp);

`

最佳答案

虫虫

在您的代码中,您找到了辅助实例,但随后立即尝试再次初始化它并抛出错误。

firebaseApp = FirebaseApp.getInstance("secondary");
FirebaseApp.initializeApp(Charts.this, firebaseOptions, "secondary").delete(); // throws IllegalStateException

它的固定形式是:

firebaseApp = FirebaseApp.getInstance("secondary");
firebaseApp.delete();

此外,在代码的最后一行,您尝试为 firebaseApp 而不是 "secondary" 的字符串值获取 FirebaseApp

secondaryDatabase = FirebaseDatabase.getInstance(firebaseApp);

固定的来源是:

secondaryDatabase = firebaseApp;
// OR
secondaryDatabase = FirebaseDatabase.getInstance("secondary");

固定代码

根本不将应用程序实例存储为 firebaseApp,只需直接删除/使用该实例即可。

List<FirebaseApp> firebaseAppList = FirebaseApp.getApps(Charts.this);

// Delete "secondary" if it exists
for (FirebaseApp app : firebaseAppList) {
if (app.getName().equals("secondary")) {
app.delete(); // found "secondary". Delete it
break;
}
}

// Build options object
FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
.setApiKey(apiKey)
.setApplicationId(appId)
.setDatabaseUrl(databaseLink)
.build();

// Initialize
secondaryDatabase = FirebaseApp.initializeApp(Charts.this, firebaseOptions, "secondary");

// secondaryDatabase is now a FirebaseApp instance for the secondary database

关于android - 如何在 android 中删除然后重新初始化 Firebase 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55180334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com