Use more than one Firebase database in one application - Swift

I have a firebase database connected to my application using GoogleService-Info.plist etc. This works great.

I would also like to connect my application to a second firebase database.

It looks like this problem has been resolved for Android here .

Any idea how to add a second firebase database with Swift in Xcode?

EDIT: I tried several approaches, including using FIROptions to create a database instance. I just can't structure the code correctly. Any help is appreciated!

+10
ios swift firebase firebase-database firebase-realtime-database
source share
3 answers

The proper way to initialize another database is to initialize another application using the FIROptions constructor, for example:

 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, and not from a huge constructor:

 let filePath = NSBundle.mainBundle().pathForResource("MyCool-GoogleService-Info", ofType:"plist") let options = FIROptions(contentsOfFile:filePath) 
+11
source share

With the latest version of Firebase you have to do:

 let filePath = Bundle.main.path(forResource: "My-GoogleService", ofType: "plist") guard let fileopts = FirebaseOptions.init(contentsOfFile: filePath!) else { assert(false, "Couldn't load config file") } FirebaseApp.configure(options: fileopts) 
+2
source share

I am trying to initialize several Firebase projects in my application, but the application receives a notification only for the first configuration.

This is my code.

 //Configure first service firebase let filePath1 = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist") guard let fileopts1 = FirebaseOptions(contentsOfFile: filePath1!) else { assert(false, "Couldn't load config file") } FirebaseApp.configure(name: "first", options: fileopts1) //Configure second service firebase let filePath = Bundle.main.path(forResource: "GoogleService-Cuarto", ofType: "plist") guard let fileopts = FirebaseOptions(contentsOfFile: filePath!) else { assert(false, "Couldn't load config file") } FirebaseApp.configure(options: fileopts) Messaging.messaging().delegate = self if #available(iOS 10.0, *) { // For iOS 10 display notification (sent via APNS) UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: {_, _ in }) } else { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) application.registerUserNotificationSettings(settings) } application.registerForRemoteNotifications() 

But Firebase my project in Firebase did not detect my application.

Help me!!!

0
source share

All Articles