Javascript 在react native firebase中调用firebase.app()的目的是什么?

Javascript 在react native firebase中调用firebase.app()的目的是什么?,javascript,firebase,react-native,react-native-firebase,Javascript,Firebase,React Native,React Native Firebase,我尝试了这两种方法,所有的方法都奏效了。有什么区别 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';

const defaultApp = firebase.app();

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


这两种方法是等效的。第二种方法仅仅依赖于一些硬编码的默认值,而第一种方法更为明确。如果您希望(例如)在单个应用程序中访问数据库,这一点尤其明显

我们的,所以我将引用:

在大多数情况下,您只需初始化一个默认应用程序。您可以通过两种等效方式访问该应用程序提供的服务:

// 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();
某些用例要求您同时创建多个应用程序。例如,您可能希望从一个Firebase项目的实时数据库中读取数据,并将文件存储在另一个项目中。或者,您可能希望对一个应用进行身份验证,而另一个应用未经身份验证。Firebase SDK允许您同时创建多个应用程序,每个应用程序都有自己的配置信息

// 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();
注意:每个应用程序实例都有自己的配置选项和身份验证状态


您不需要调用该方法,除非您在应用程序中使用多个firebase应用程序实例

一针见血,除此之外,在react native firebase中,默认应用程序通过google services json/plist文件在本机计数器部件firebase sdk(ios和android)上预先初始化,因此,默认应用程序不需要InitializeApp-它仍然允许您调用它,但在内部它会忽略传递的所有选项,并返回已初始化的应用程序(带有弃用警告)。这将在将来的版本中更改为抛出“默认应用程序已初始化”错误-与web sdk相同。免责声明:RNFirebase的作者
// 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();