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); }
All of the above is an attempt to connect to a second Firebase App.
If you want to know if the client is connected to the server before calling setValue() , you can attach a listener to . info/connected .
Instance ID provides a unique identifier for each instance of your app and a mechanism to authenticate and authorize actions, like sending messages via Firebase Cloud Messaging. The InstanceID is long lived, but may expire for the following reasons: Device factory reset.
On firebase web, you check if already initialized with:
if (firebase.apps.length === 0) { firebase.initializeApp({}); }
In v9, Firebase has been modularized for better tree shaking. So we can no longer import entire app and check the apps
property AFAIK. The below approach can be used instead.
import { initializeApp, getApps, getApp } from "firebase/app"; getApps().length === 0 ? initializeApp(firebaseConfig) : getApp();
https://firebase.google.com/docs/reference/js/v9/app.md#getapps for documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With