2

I tried both and all working. What are the difference?

import firebase from 'react-native-firebase';

const defaultApp = firebase.app();

defaultApp.database().ref('foobar').once('value', (snapshot) => {
  // snapshot from default app
});

vs

import firebase from 'react-native-firebase';

firebase.database().ref('foobar').once('value', (snapshot) => {
  // snapshot from default app
});

2 Answers 2

4

The two approaches are equivalent. The second one just relies on some hard-coded defaults, while the first is more explicit. This becomes especially apparent if you want to (for example) access to databases in a single app.

Our documentation explains this rather well, so I'll quote from there:

In most cases, you will only have to initialize a single, default app. You can access services off of that app in two equivalent ways:

// Initialize the default app
var defaultApp = firebase.initializeApp(defaultAppConfig);

console.log(defaultApp.name);  // "[DEFAULT]"

// You can retrieve services via the defaultApp variable...
var defaultStorage = defaultApp.storage();
var defaultDatabase = defaultApp.database();

// ... or you can use the equivalent shorthand notation
defaultStorage = firebase.storage();
defaultDatabase = firebase.database();

Some use cases require you to create multiple apps at the same time. For example, you might want to read data from the Realtime Database of one Firebase project and store files in another project. Or you might want to authenticate one app while have another app be unauthenticated. The Firebase SDK allows you create multiple apps at the same time, each with their own configuration information.

// Initialize the default app
firebase.initializeApp(defaultAppConfig);

// Initialize another app with a different config
var otherApp = firebase.initializeApp(otherAppConfig, "other");

console.log(firebase.app().name);  // "[DEFAULT]"
console.log(otherApp.name);        // "other"

// Use the shorthand notation to retrieve the default app's services
var defaultStorage = firebase.storage();
var defaultDatabase = firebase.database();

// Use the otherApp variable to retrieve the other app's services
var otherStorage = otherApp.storage();
var otherDatabase = otherApp.database();

Note: Each app instance has its own configuration options and authentication state.

Sign up to request clarification or add additional context in comments.

1 Comment

Hit the nail on the head there, just to add to this, in react-native-firebase the default app is pre-initialised on the native counter part firebase sdk's (ios & android) via the google services json/plist files, therefore initialializeApp is not required for the default app - it will still let you call it, but internally it ignores all options passed and will return the already initialized app (with a deprecation warning). This will change in a future version to throw a 'default app already initialized' error - same as the web sdk. Disclaimer: Author of RNFirebase
0

you do not need to call that method, unless you are using more than one firebase app instance in your application

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.