我正在尝试开发一个应用程序,可以在其中将信息保存在两个不同的Firebase实时数据库中。目前,我的应用已连接到一个数据库(运行良好)。我使用了以下代码:
private DatabaseReference RootRef;
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("******") // Required for Analytics.
.setApiKey("******") // Required for Auth.
.setDatabaseUrl("********") // Required for RTDB.
.build();
FirebaseApp.initializeApp(this /* Context */, options, "secondary");
FirebaseApp app = FirebaseApp.getInstance("secondary");
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(app);
RootRef = secondaryDatabase.getReference();
RootRef.child("Groups").child("test").setValue("").addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful())
Toast.makeText(MainActivity.this, "Group created", Toast.LENGTH_LONG).show();
}
});
(我当然用新项目的数据填充***)
但是当我尝试使用第二个数据库时,似乎没有任何反应。对于我执行的每个操作,它只会影响第一个数据库。请帮忙!
谢谢!
答案 0 :(得分:0)
Java
// Get the default database instance for an app
DatabaseReference primary = FirebaseDatabase.getInstance()
.getReference();
// Get a secondary database instance by URL
DatabaseReference secondary = FirebaseDatabase.getInstance("https://testapp-1234.firebaseio.com")
.getReference();
科特琳
// Get the default database instance for an app
val primary = FirebaseDatabase.getInstance().reference
// Get a secondary database instance by URL
val secondary = FirebaseDatabase
.getInstance("https://testapp-1234.firebaseio.com").reference