2

I am currently trying to add a second firebase Real Time Database to my application. I am following this tutorial from the Firebase blog which is exactly what I want. I am currently connected to the other Database. However I am unable to create a reference

eg: mRef = FirebaseDatabase.getInstance().getReference();

I am unsure of how to intialise the new database to utilise the information inside.

        mRef = FirebaseDatabase.getInstance().getReference();
        Intent intent = getIntent();

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setApplicationId("AppID") // Required for Analytics.
                .setApiKey("APIKEY") // Required for Auth.
                .setDatabaseUrl("DatabaseURL") // Required for RTDB.

                .build();
        FirebaseApp.initializeApp(this, options, "secondary");

// Retrieve my other app.
        FirebaseApp app = FirebaseApp.getInstance("secondary");
// Get the database for the other app.
        FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(app);

I have tried and I'm able to obtain the URL of the new database by using app.getOptions.getDatabaseURL, but I'm not able to use it to access information in the new Database.

I've also read through the Firebase docs on android but I can't seem to find anything that works!

Any help would be greatly appreciated!!! Thanks :)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Yenn Tan
  • 43
  • 3

1 Answers1

1

initialize a second FirebaseApp object with explicit options in your code:

FirebaseOptions options = new FirebaseOptions.Builder()
        .setApiKey("AI...j0")
        .setApplicationId("1:5...e0")
        .setDatabaseUrl("https://myapp.firebaseio.com")
        .build();
FirebaseApp secondApp = FirebaseApp.initializeApp(getApplicationContext(), options, "second app");
FirebaseDatabase secondDatabase = FirebaseDatabase.getInstance(secondApp);
secondDatabase.getReference().setValue(ServerValue.TIMESTAMP);

I got the configuration values from the second project's google-services.json. The API Key is under a property called api_key, the Application ID came from a property called mobilesdk_app_id and the database URL came from a property called firebase_url.

Jai Khambhayta
  • 4,198
  • 2
  • 22
  • 29
  • Hi, I was actually referring to something along the lines of structuring it like " mRef = FirebaseDatabase.getInstance().getReference();". So that i can use "mRef" to write and read data For example, mRef.child("eg1").child("eg2").setValue("Testing"); Is it possible to create mRef2 that utilises the new database? Sorry if my explanation wasn't clear enough! – Yenn Tan May 29 '17 at 08:18
  • @jai: don't just copy an answer from [another question](https://stackoverflow.com/questions/37634767/how-to-connect-to-more-than-one-firebase-database-from-an-android-app). If the answer is the same, the question is a duplicate. – Frank van Puffelen May 29 '17 at 14:36
  • Hi @FrankvanPuffelen! Sorry if this question is a duplicate, but how would i go about reading/writing from the second database. Currently I am using mRef.child("testing123").setValue("123"); to write to the first Database, with mRef being mRef = FirebaseDatabase.getInstance().getReference(); Would there be something along the lines of another firebase reference like mRef2? I am unable to find anything related inside the official firebase docs – Yenn Tan May 29 '17 at 16:17
  • 1
    Make sure you get the reference to the second database from the second app instance. `mRef2 = secondaryDatabase.getRef()`. – Frank van Puffelen May 29 '17 at 16:24
  • @FrankvanPuffelen I've just tested it out and it works! Thanks a lot I really appreciate it :D – Yenn Tan May 29 '17 at 17:03
  • @FrankvanPuffelen Hi! Sorry to bother you, but I have another issue with multiple database. I encountered the error "FirebaseApp name [DEFAULT] already exists". I have read the other thread and tried to implement the solution that you have offered. However, I can't seem to get it to work. What I understand is that I am initializing the second database multiple times, causing the android app to crash. When I try this, – Yenn Tan Jun 21 '17 at 07:14
  • "If(FirebaseApp.getInstance("secondApp") == null){ if(FirebaseApp.getInstance("secondApp") == null){ FirebaseApp secondApp = FirebaseApp.initializeApp(getApplicationContext(), options, "secondApp"); FirebaseDatabase secondDatabase = FirebaseDatabase.getInstance(secondApp); mRef2 = secondDatabase.getReference(); } }" FirebaseApp with name secondApp doesn't exist. – Yenn Tan Jun 21 '17 at 07:18