10

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!

Community
  • 1
  • 1
tsteve
  • 549
  • 1
  • 7
  • 16

2 Answers2

12

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)
Mike McDonald
  • 15,609
  • 2
  • 46
  • 49
  • Thanks, Mike. I'm glad it's possible. – tsteve Jul 18 '16 at 18:03
  • Mike, how do we get notified when the release with this feature is released? – frank Sep 01 '16 at 06:47
  • It's all in the release notes: https://firebase.google.com/support/releases. BTW, the method you want is here: https://firebase.google.com/docs/reference/ios/firebaseanalytics/interface_f_i_r_options.html#a9a699abf436a6fcf9fd8165a2dfceaf6 – Mike McDonald Sep 02 '16 at 01:02
  • 1
    I have Firebase analytics 3.2.0 and Firebase 3.2.1 , initWithContentsOfFile method is not available – EhsanR Sep 21 '16 at 22:41
  • 1
    @EhsanR you should update to Firebase 3.6.0, which is the latest and greatest :) – Mike McDonald Sep 22 '16 at 15:17
  • @MikeMcDonald could you please help me out with an answer to my related question https://stackoverflow.com/questions/44866220 – bibscy Jul 02 '17 at 13:26
2

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)
DiegoQ
  • 1,114
  • 11
  • 20