Java 如何检查 Firebase 应用程序是否已在 Android 上初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37652328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a Firebase App is already initialized on Android
提问by Relm
With the following, the first time it's called it works, but then fails on subsequent calls with "FirebaseApp name [DEFAULT] already exists!"
使用以下内容,第一次调用它时可以工作,但在随后的调用中失败,并显示“FirebaseApp 名称 [DEFAULT] 已经存在!”
public FirebaseDatabase conn(Context c) {
FirebaseOptions options = new FirebaseOptions.Builder()
.setApiKey("key")
.setDatabaseUrl("url")
.setApplicationId("ID")
.build();
/////I tried Try and Catch with no success//////
FirebaseApp app = FirebaseApp.initializeApp(c, options);
/// for this : FirebaseApp app = FirebaseApp.initializeApp(c, options, "some_app");
//// will fail with "FirebaseApp name some_app already exists!"
return FirebaseDatabase.getInstance(app);
}
All of the above is an attempt to connect to a second Firebase App.
以上所有内容都是尝试连接到第二个 Firebase 应用程序。
采纳答案by Daniel Laurindo
On firebase web, you check if already initialized with:
在 firebase web 上,您检查是否已经初始化:
if (firebase.apps.length === 0) {
firebase.initializeApp({});
}
回答by KCE
I think what you want to do is check the list of running apps before initializing your app. Each of the SDKs have a method for getting this array, in android it's getApps
:
我认为您想要做的是在初始化您的应用程序之前检查正在运行的应用程序列表。每个 SDK 都有一个获取这个数组的方法,在 android 中它是getApps
:
https://firebase.google.com/docs/reference/android/com/google/firebase/FirebaseApp.html
https://firebase.google.com/docs/reference/android/com/google/firebase/FirebaseApp.html
Then you can check to see if your app is already initialized.
然后你可以检查你的应用程序是否已经初始化。
In my case I just ended up checking the length of the array (I'm using the javascript / web sdk so I'm sure it's a little different for Android) and initializing a new app if it is 0.
在我的情况下,我最终检查了数组的长度(我使用的是 javascript / web sdk,所以我确定它对于 Android 有点不同)并在它为 0 时初始化一个新应用程序。
回答by Harendra Kr. Jadon
I faced the similar issue, I resolved it as following:
我遇到了类似的问题,我解决了以下问题:
- Create a var for the application and initialize it with null
- Take reference of the application while initialization
- Check before initializing it again
- 为应用程序创建一个 var 并用 null 初始化它
- 初始化时参考应用程序
- 在再次初始化之前检查
//global variable
var firebaseResumeDownloadAdd = null;
//inside function check before initializing
if(firebaseResumeDownloadAdd==null){
firebaseResumeDownloadAdd =
firebase.initializeApp(functions.config().firebase);
}
回答by Manoj Pathak
I faced the similar issue. I solved the following problem by deleting the already initialized app.
我遇到了类似的问题。我通过删除已经初始化的应用程序解决了以下问题。
// Access your firebase app
let app = firebase.app();
// Delete your app.
app.delete(app);
Solution works for web.
解决方案适用于网络。
回答by Jorge Arimany
You can try to get the Firebase app instance, in it's code firebase checks if it's initialized, if not it throws an IllegalStateException
.
您可以尝试获取 Firebase 应用实例,在它的代码中,firebase 检查它是否已初始化,如果没有,它会抛出一个IllegalStateException
.
try{
FirebaseApp.getInstance();
}
catch (IllegalStateException e)
{
//Firebase not initialized automatically, do it manually
FirebaseApp.initializeApp(this);
}
回答by Nick Cardoso
For those wondering how to do the same as the accepted answer, in Android:
对于那些想知道如何在 Android 中执行与已接受答案相同的操作的人:
if (FirebaseApp.getApps(context).isEmpty()) {
FirebaseApp.initializeApp(context);
}
and in an instrumented test environment, use this context:
在仪器化测试环境中,使用以下上下文:
InstrumentationRegistry.getContext()
回答by rMili
Not sure in android, but how about using a singleton method. In JS you can do this. Hope this helps someone
在 android 中不确定,但是如何使用单例方法。在 JS 中你可以做到这一点。希望这有助于某人
// Config file
import * as firebase from "firebase";
const config = {...};
export default !firebase.apps.length ? firebase.initializeApp(config) : firebase.app();
// Other file
import firebase from '../firebase';
回答by edyrkaj
If you are using Nativescript to build an hybrid mobile app for Android | iOS you can use this script:
如果您正在使用 Nativescript 为 Android 构建混合移动应用程序 | iOS 你可以使用这个脚本:
import * as firebase from 'nativescript-plugin-firebase';
_initFirebase(): Promise<any> {
if (!(firebase as any).initialized) {
return firebase.init({}).then(
() => {
console.info('firebase started...');
},
(error) => console.error(error)
);
}
}