如何检查 Firebase 应用程序是否已在 Android 上初始化 [英] How to check if a Firebase App is already initialized on Android

查看:30
本文介绍了如何检查 Firebase 应用程序是否已在 Android 上初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下内容,第一次调用它可以工作,但随后调用失败并显示FirebaseApp 名称 [DEFAULT] 已经存在!"

With the following, the first time it's called it works, but then fails on subsequent calls with "FirebaseApp name [DEFAULT] already exists!"

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);
}

以上所有内容都是尝试连接到第二个 Firebase 应用.

All of the above is an attempt to connect to a second Firebase App.

推荐答案

在 firebase web 上,您检查是否已经初始化:

On firebase web, you check if already initialized with:

if (firebase.apps.length === 0) {
    firebase.initializeApp({});
}

这篇关于如何检查 Firebase 应用程序是否已在 Android 上初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆