单个 android 应用程序中的多个 Firebase 项目
Multiple Firebase projects in a single android application
我正在尝试使用 Firebase 构建一个 android 应用程序,该应用程序需要在单个应用程序中有两个独立的数据库(教师和学生)。我在整个互联网上搜索了解决方案,但我得到的只是这个解决方案 - ,这似乎是一个很好的解决方案,但我不明白。
那么,如何在单个应用程序中合并两个 firebase 项目?
您不再需要实施 link 中描述的社区解决方案。
根据this:
Sometimes you need to access different projects using the same APIs - for example, accessing multiple database instances. In most cases there is a central Firebase application object that manages the configuration for all the Firebase APIs. This object is initialized as part of your normal setup. However, when you want to access multiple projects from a single application, you’ll need a distinct Firebase application object to reference each one individually. It’s up to you to initialize these other instances.
您需要先创建一个 Firebase 选项对象来保存 Firebase 应用程序的配置数据,如下所示:
// Manually configure Firebase Options. The following fields are REQUIRED:
// - Project ID
// - App ID
// - API Key
FirebaseOptions options = new FirebaseOptions.Builder()
.setProjectId("my-firebase-project")
.setApplicationId("1:27992087142:android:ce3b6448250083d1")
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
// setDatabaseURL(...)
// setStorageBucket(...)
.build();
初始化此选项对象后,您可以使用它来配置额外的 Firebase 应用程序实例。
例如,如果您有另一个名为“otherApp”的应用实例,您可以这样做:
// Initialize with secondary app
FirebaseApp.initializeApp(this /* Context */, options, "otherApp");
// Retrieve secondary FirebaseApp
FirebaseApp secondary = FirebaseApp.getInstance("otherApp");
通过这种方式,您可以连接到另一个实时数据库。
是的,您可以按照该指南进行操作 https://firebase.google.com/docs/projects/multiprojects#java。
基本上,您需要手动启动提供配置的 firebase 应用程序
// [START firebase_options]
// Manually configure Firebase Options. The following fields are REQUIRED:
// - Project ID
// - App ID
// - API Key
FirebaseOptions options1 = new FirebaseOptions.Builder()
.setProjectId("my-firebase-project")
.setApplicationId("1:27992087142:android:ce3b6448250083d1")
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
// setDatabaseURL(...)
// setStorageBucket(...)
.build();
FirebaseOptions options2 = new FirebaseOptions.Builder()
.setProjectId("my-firebase-project")
.setApplicationId("1:27992087142:android:ce3b6448250083d1")
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
// setDatabaseURL(...)
// setStorageBucket(...)
.build();
// [END firebase_options]
// [START firebase_secondary]
// Initialize with secondary app
FirebaseApp.initializeApp(this /* Context */, options1, "first");
FirebaseApp.initializeApp(this /* Context */, options2, "secondary");
FirebaseApp first = FirebaseApp.getInstance("first");
FirebaseApp secondary = FirebaseApp.getInstance("secondary");
您可以从 google-service.json
获取所有需要的数据
稍后您可以从那个 FirebaseApp
获取数据库,例如
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(secondary);
然后你就可以像以前一样处理这个项目了。
你提到的例子可以用在下面的方法中。
你可以在firebase db中创建两个主表然后你可以通过
进行操作
val reference = FirebaseDatabase.getInstance().getReference()
val teacher = reference.child("teacher")//refers to the teacher table
val student = reference.child("student")//refers to the student table
我正在尝试使用 Firebase 构建一个 android 应用程序,该应用程序需要在单个应用程序中有两个独立的数据库(教师和学生)。我在整个互联网上搜索了解决方案,但我得到的只是这个解决方案 -
那么,如何在单个应用程序中合并两个 firebase 项目?
您不再需要实施 link 中描述的社区解决方案。
根据this:
Sometimes you need to access different projects using the same APIs - for example, accessing multiple database instances. In most cases there is a central Firebase application object that manages the configuration for all the Firebase APIs. This object is initialized as part of your normal setup. However, when you want to access multiple projects from a single application, you’ll need a distinct Firebase application object to reference each one individually. It’s up to you to initialize these other instances.
您需要先创建一个 Firebase 选项对象来保存 Firebase 应用程序的配置数据,如下所示:
// Manually configure Firebase Options. The following fields are REQUIRED:
// - Project ID
// - App ID
// - API Key
FirebaseOptions options = new FirebaseOptions.Builder()
.setProjectId("my-firebase-project")
.setApplicationId("1:27992087142:android:ce3b6448250083d1")
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
// setDatabaseURL(...)
// setStorageBucket(...)
.build();
初始化此选项对象后,您可以使用它来配置额外的 Firebase 应用程序实例。
例如,如果您有另一个名为“otherApp”的应用实例,您可以这样做:
// Initialize with secondary app
FirebaseApp.initializeApp(this /* Context */, options, "otherApp");
// Retrieve secondary FirebaseApp
FirebaseApp secondary = FirebaseApp.getInstance("otherApp");
通过这种方式,您可以连接到另一个实时数据库。
是的,您可以按照该指南进行操作 https://firebase.google.com/docs/projects/multiprojects#java。
基本上,您需要手动启动提供配置的 firebase 应用程序
// [START firebase_options]
// Manually configure Firebase Options. The following fields are REQUIRED:
// - Project ID
// - App ID
// - API Key
FirebaseOptions options1 = new FirebaseOptions.Builder()
.setProjectId("my-firebase-project")
.setApplicationId("1:27992087142:android:ce3b6448250083d1")
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
// setDatabaseURL(...)
// setStorageBucket(...)
.build();
FirebaseOptions options2 = new FirebaseOptions.Builder()
.setProjectId("my-firebase-project")
.setApplicationId("1:27992087142:android:ce3b6448250083d1")
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
// setDatabaseURL(...)
// setStorageBucket(...)
.build();
// [END firebase_options]
// [START firebase_secondary]
// Initialize with secondary app
FirebaseApp.initializeApp(this /* Context */, options1, "first");
FirebaseApp.initializeApp(this /* Context */, options2, "secondary");
FirebaseApp first = FirebaseApp.getInstance("first");
FirebaseApp secondary = FirebaseApp.getInstance("secondary");
您可以从 google-service.json
稍后您可以从那个 FirebaseApp
获取数据库,例如
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(secondary);
然后你就可以像以前一样处理这个项目了。
你提到的例子可以用在下面的方法中。 你可以在firebase db中创建两个主表然后你可以通过
进行操作val reference = FirebaseDatabase.getInstance().getReference()
val teacher = reference.child("teacher")//refers to the teacher table
val student = reference.child("student")//refers to the student table