Can you have more than one iOS application utilizing the same Firebase back end?
You can add 30 apps to a single project to share data, as described in the documentation.
Firebase restricts the total number of Firebase Apps within a Firebase project to 30.
See also:
- Multiple apps in one project or one project per app in Firebase?
- Pros and cons of using one project for multiple apps in Firebase Could Messaging
- Adding multiple apps in a firebase project
- Add multiple apps to single project in Firebase
- Is there a way to have 1 Firebase database for 2 apps with different package names?
- https://groups.google.com/g/firebase-talk/c/VAtoKx6qJjw
Use more than one Firebase database in single app - Swift
The proper way to initialize another database is to initialize another app, using the FIROptions
constructor, like so:
FIRDatabase().database() // gets you the default database
let options = FIROptions(googleAppID: bundleID: , GCMSenderID: , APIKey: , clientID: , trackingID: , androidClientID: , databaseURL: "https://othernamespace.firebaseio.com", storageBucket: , deepLinkURLScheme: ) // fill in all the other fields
FIRApp.configureWithName("anotherClient", options: options)
let app = FIRApp(named: "anotherClient")
FIRDatabase.database(app: app!) // gets you the named other database
Or you can initialize from another named plist rather than a huge constructor:
let filePath = NSBundle.mainBundle().pathForResource("MyCool-GoogleService-Info", ofType:"plist")
let options = FIROptions(contentsOfFile:filePath)
How to connect to more than one firebase database from an android App
You'll need to 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
.
Also see the documentation on using multiple projects in your application.
More databases inside the same Firebase Project
Multi-database is a new feature that allows you to create multiple database instances.
To get started you need to be in the Blaze plan. In the data viewer you can click the triple dot icon to create new database instances:
To access data from a secondary instance you use an absolute URL when creating a database instance.
const app = firebase.initializeApp({
// use your main config
databaseUrl: "https://multi-db.firebaseio.com/"
});
const db1 = app.database(); // This is the default DB
const db2 = app.database("https://multi-db501c7.firebaseio.com/");
Since these databases are in the same project they share the same Authentication session.
Also each database instance has its own set of security rules and the databases can handle different structures.
Firebase Firestore use two different instances in the same app
It is definitely possible to access multiple Firestore databases (or other Firebase resources) in a single application. But only one of them can be initialized from the GoogleService-Info.plist
. The other one(s) you will have to initialize from within your code, based on the information in the secondary GoogleService-Info.plist
.
The basic approach for this is to first create a FirebaseOptions
object with the configuration data of the second project:
// Configure with manual options.
let secondaryOptions = FirebaseOptions(googleAppID: "1:27992087142:ios:2a4732a34787067a", gcmSenderID: "27992087142")
secondaryOptions.bundleID = "com.google.firebase.devrel.FiroptionConfiguration"
secondaryOptions.apiKey = "AIzaSyBicqfAZPvMgC7NZkjayUEsrepxuXzZDsk"
secondaryOptions.clientID = "27992087142-ola6qe637ulk8780vl8mo5vogegkm23n.apps.googleusercontent.com"
secondaryOptions.databaseURL = "https://myproject.firebaseio.com"
secondaryOptions.storageBucket = "myproject.appspot.com"
And then use that to initialize a secondary App
object to get the Firebase service(s) you need:
// Configure an alternative FIRApp.
FirebaseApp.configure(name: "secondary", options: secondaryOptions)
// Retrieve a previous created named app.
guard let secondary = FirebaseApp.app(name: "secondary")
else { assert(false, "Could not retrieve secondary app") }
let secondaryDb = Firestore.firestore(app: secondary)
Also see:
- The Firebase documentation on using multiple projects in your application.
Multiple Firebase projects in one app
I recommend you to keep all the communities together. First, because I'm not really sure that you can use multiple Firebase Projects in the same app.
I suggest you to store this "communities" as items in your database with all their current info. For example:
{
"Communities":{
"CommunityKey1":{
"communityName":
"createdDate":
....
}
"CommunityKey2":{
"communityName":
"createdDate":
....
}
"CommunityKey3":{
"communityName":
"createdDate":
....
}
....
}
}
And when you add your users to your database, just add them an associated field "currentCommunity" or something like that where you store the associated communityID for that user. Then, when you want to filter the users for one community just use:
FirebaseDatabase.getInstance().getReference().child("users").child("currentCommunity");
You can filter else more using .equalTo("CommunityID")
at the end of that query. I'm not an expert in Firebase, but I'm doing an app with a similar kind of user filtering(using groups instead of communities and they can belong to more than one), and the best way to do this was using this model.
Firebase two database (iOS, Android)
From the Firebase documentation on connecting your app to multiple database instances:
Swift:
// Get the default database instance for an app
var ref: DatabaseReference!
ref = Database.database().reference()
// Get a secondary database instance by URL
var ref: DatabaseReference!
ref = Database.database("https://testapp-1234.firebaseio.com").reference()
Android:
// Get the default database instance for an app
private DatabaseReference mDatabase;
// ...
mDatabase = FirebaseDatabase.getInstance().getReference();
// Get a secondary database instance by URL
private DatabaseReference mDatabase;
// ...
mDatabase = FirebaseDatabase.getInstance("https://testapp-1234.firebaseio.com").getReference();
Note that it is more common to use separate projects for different environments. By having each environment on a separate project, they also get their own set of users, their own access tokens, and are separated in many more ways.
Related Topics
Ios10, Swift 3, and Fcm Delegate Error
Hidden Property Cannot Be Changed Within an Animation Block
Uitableview Inside Uitableviewcell with Dynamic Height
Cmpedometer Querypedometerdatafromdate Returns Error 103
How to Calculate a Random Cgpoint Which Does Not Touch a Uiview
Ios10, Swift 3, and Fcm Delegate Error
Uidropinteractiondelegate Performdrop Not Called
Exc_Bad_Access in Parent Class Init() with Xcode 10.2
Show Posts from the Users You Are Following - Swift
How to Send Different Users to Separate View Controllers Using Firebase and Xcode
How to Download a PDF File in Swift and Find in File Manager
Pausing Spritekit Game Using Appdelegate in iOS8
Creating Tableview Sections from JSON Data Swift 4
iOS Swift Detecting Image Rotation
Swiftui Multiple Navigationlinks in Form/Sheet - Entry Stays Highlighted
Uiimage Aspect Fill When Using Drawinrect
Swiftui - Navigationview Title and Back Button Clipped Under Status Bar After Orientation Change