在单个应用程序中使用多个Firebase数据库 – Swift

我有一个firebase数据库目前已经连接到我的应用程序与谷歌服务Info.plist等,它的效果很好。

我想将我的应用连接到第二个Firebase数据库。

看起来这个问题已经解决了在Android 这里 。

任何想法如何在Xcode中使用Swift添加第二个Firebase数据库?

编辑:我已经尝试了几种方法,包括使用FIROptions来build立一个数据库的实例。 我似乎无法正确构build代码。 任何帮助表示赞赏!

初始化另一个数据库的正确方法是使用FIROptions构造函数初始化另一个应用程序,如下所示:

 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 

或者你可以从另一个命名plist而不是一个巨大的构造函数初始化:

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

使用最新版本的Firebase,您应该这样做:

 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)