Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use more than one Firebase database in single app - Swift

I have a firebase database currently connected to my app with the GoogleService-Info.plist, etc. It works great.

I would like to connect my app to a second firebase database as well.

It looks like this problem was solved for Android here.

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

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

like image 417
tsteve Avatar asked Jul 13 '16 23:07

tsteve


2 Answers

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)
like image 108
Mike McDonald Avatar answered Nov 15 '22 19:11

Mike McDonald


With the newest version of Firebase you should 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)
like image 24
DiegoQ Avatar answered Nov 15 '22 18:11

DiegoQ