Skip to content

Instantly share code, notes, and snippets.

@katowulf
Created September 9, 2019 21:03
Show Gist options
  • Save katowulf/298f5bc5ac164bf4284cf469bce5d732 to your computer and use it in GitHub Desktop.
Save katowulf/298f5bc5ac164bf4284cf469bce5d732 to your computer and use it in GitHub Desktop.
Get multiple app instances of Firestore in Firebase SDKs. More on multiple environments here: https://firebase.google.com/docs/projects/multiprojects
FirebaseApp primary = FirebaseApp.getInstance();
FirebaseFirestore primaryDatabase = FirebaseFirestore.getInstance();
// Reference the same setup/instance
FirebaseOptions options = primary.getOptions();
/*
// Use multiple projects/environments. More here: https://firebase.google.com/docs/projects/multiprojects
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("1:27992087142:android:ce3b6448250083d1") // Required for Analytics.
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw") // Required for Auth.
.setDatabaseUrl("https://myproject.firebaseio.com") // Required for RTDB.
.build();
*/
// Initialize with secondary app.
FirebaseApp.initializeApp(this /* Context */, options, "secondary");
// Retrieve secondary app.
FirebaseApp secondary = FirebaseApp.getInstance("secondary");
// Get the database for the other app.
FirebaseFirestore secondaryDatabase = FirebaseFirestore.getInstance(secondary);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment