Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Firebase projects in one app

I'm building an app that has an arbitrary amount of 'micro-communities' on it. Currently a user can only belong to one, so when they initially sign up, they enter a secret code which associates them with the relevant community.

Is it wise to separate these 'micro-communities' (which are owned by future clients playing for a space on the app) into separate Firebase projects or keep all the data together?

like image 431
Daniel van der Merwe Avatar asked Sep 22 '16 06:09

Daniel van der Merwe


1 Answers

Firebase now supports accessing multiple projects in one app. No need to go the micro-community route. Here is the tutorial on how to do it: https://firebase.google.com/docs/configure/

I'm currently using it in my project in Android, so if you are having trouble setting it up and are implementing it in Android, feel free to drop a comment!

EDIT

When you configure Firebase with your app you are defaulted to one database account. If you would like to use a second one (or more), continue reading.

In your activity or fragment, you must initialize a FirebaseOptions instance with all your account info, initialize a FirebaseApp with a tag name that represents the second account, then get the instance of your FirebaseApp, and lastly, get a DatabaseReference instance of your second account so you may use it as you like. Here is the block of code I use: (NOTE: the information you enter as the parameters for you FirebaseOptions instance is associated with your SECOND database account):

private void initSecondFirebaseAcct()
{
    FirebaseOptions options = new FirebaseOptions.Builder()
            .setApplicationId("<your application id>")
            .setApiKey("<your api key>")
            .setDatabaseUrl("<your DB url that ends in 'firebaseio.com/' ")
            .build();

    try
    {
        FirebaseApp.initializeApp(this, options, "<database tag>");
    }
    catch (Exception e)
    {
        Log.d("Firebase error", "App already exists");
    }

    mMySecondApp = FirebaseApp.getInstance("<database tag>");
    mSecondDBRef = FirebaseDatabase.getInstance(mMySecondApp).getReference();
}

Hope that helps!

like image 156
David Velasquez Avatar answered Sep 28 '22 06:09

David Velasquez