0

I am currently creating an app in Xcode using swift where I want to read and write from two different Firebase databases in one Xcode project. I know that this is definitely supported by Firebase, but when I use their documentation I am still finding it hard to set up.

I know there is an answer for this question using Android here: https://stackoverflow.com/a/43215981/9308748

However, I am making my app for IOS.

I have looked at the documentation and followed what it says, but whenever I run the project it tries to get all information I am looking for from the most recently created Firebase database, and not the specific one I am trying to retrieve from. I don't know what code to put in the AppDelegate class and what to put in the viewController classes in order to connect to a specific database.

I have added both GoogleService-info.plist Files into my Xcode project (I don't know if I should do this or not)

This is what I have done so far inside my AppDelegate Class for both of my firebase projects:

    let primaryOptions = FirebaseOptions(googleAppID: "My googleAppID", gcmSenderID: "my gcmSender")
    primaryOptions.bundleID = "my bundleID"
    primaryOptions.apiKey = "my apiKey"
    primaryOptions.clientID = "my clientID"
    primaryOptions.databaseURL = "my DatabaseURL"
    primaryOptions.storageBucket = "my storageBucket"

    FirebaseApp.configure(name: "primaryOptions", options: primaryOptions)

    guard let primaryApp = FirebaseApp.app(name: "primaryOptions")
        else { assert(false, "Could Not retrieve primary app")}

    let primaryDB = Database.database(app: primaryApp)

Then after doing this in my AppDelegate Class I don't know how to reference this specific set of information form my view controller classes in order to read and write from the specific database.

Please Help!

3 Answers3

0

After configuring all apps in your AppDelegate there is two methods that can help you in your entire code (you can use it in any viewController)

1) app(name:)

Returns a previously created FIRApp instance with the given name, or nil if no such app exists. This method is thread safe.

Declaration

class func app(name: String) -> FirebaseApp?

Example:

guard let primaryApp = FirebaseApp.app(name: "primaryOptions") else { return }

2) allApps()

Returns the set of all extant FIRApp instances, or nil if there are no FIRApp instances. This method is thread safe.

Declaration

class func allApps() -> [String : FirebaseApp]?

Example:

guard let allApps = FirebaseApp.allApps, let primaryApp = allApps["primaryOptions"] else { return }

after getting your FirbaseApp instance you can get database reference

Database.database(app: primaryApp).reference()
iAmrSalman
  • 621
  • 8
  • 9
  • where do I write the `class func app(name: String) -> FirebaseApp?`. In my appDelegate or my viewController? When I put that exact line of code into my view controller it gives errors – Peter Brink Mar 17 '18 at 18:02
  • Same question for the `guard let primaryApp = FirebaseApp.app(name: "primaryOptions") else { return }`. It keeps saying "Non-void function should return a value" and whenever I change it I get new errors. I just cannot get it to work please can you give a more detailed description of the code needed and where I actually have to put it. Thank you – Peter Brink Mar 17 '18 at 18:38
0

As got it to work using Swift 4.2, I am posting the solution for that.

Its is used in AppDelegate as explained by firebase, but if you are using login or other stuff for firstDB I suggest you process it like I did.

Got the code from here: https://github.com/firebase/snippets-ios/blob/master/firoptions/FiroptionConfiguration/FiroptionConfiguration/AppDelegate.swift

in my case, Im processing login and authentication through FirstBD and then later I am retrieving data from secondDB.

1- First add secondDB plist:

My-GoogleService.plist

2- I created a function to delete the firstDB configure and configured secondBD as below:

    private func configSecondDatabase(){
      FirebaseApp.app()?.delete({ (success) in

      }) // Delete FirstDB as we recreate below.

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

      let secondaryOptions = FirebaseOptions(googleAppID: "yourGoogleAppID", gcmSenderID: "yourGcmSenderID")
      secondaryOptions.bundleID = "yourBundleID"
      secondaryOptions.apiKey = "yourApiKey"
      secondaryOptions.clientID = "yourClientID"
      secondaryOptions.databaseURL = "yourDatabaseURL"
      secondaryOptions.storageBucket = "yourStorageBucket"

      // 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 = Database.database(app: secondary)
      let defaultDb = Database.database()

      guard let defapp = FirebaseApp.app()
         else { assert(false, "Could not retrieve default app") }

      assert(secondaryDb.app == secondary)
      assert(defaultDb.app == defapp)

   }

And called it in the ViewController viewDidLoad() where I wanted to process secondDB:

override func viewDidLoad() {
   configSecondDatabase()
   let ref = Database.database().reference() // reference of secondDB

}

Hope this will help someone, and saves his time. Thanks

Mazen Sa
  • 19
  • 1
0

If you don't really need to switch between two databases and just need to use a new database different from the default, go to your GoogleService-Info.plist and change the DATABASE_URL value to the URL of the firebase database you want to use.