我目前正在使用swift在Xcode中创建一个应用程序,我希望在一个Xcode项目中从两个不同的Firebase数据库进行读写。我知道Firebase肯定支持这一点,但是当我使用他们的文档时,我仍然觉得很难设置。
我知道在这里使用Android有这个问题的答案:https://stackoverflow.com/a/43215981/9308748
但是,我正在为IOS制作我的应用程序。
我查看了文档并按照它说的内容,但每当我运行项目时,它都会尝试从最近创建的Firebase数据库中获取我要查找的所有信息,而不是我尝试从中检索的特定信息。 。我不知道在AppDelegate类中放入什么代码以及在viewController类中放置什么以连接到特定数据库。
我已将两个GoogleService-info.plist文件添加到我的Xcode项目中(我不知道是否应该这样做)
这是我在AppDelegate Class中为我的两个firebase项目所做的工作:
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)
然后在我的AppDelegate类中执行此操作后,我不知道如何从我的视图控制器类中引用这组特定的信息,以便从特定数据库中读取和写入。
请帮助!
答案 0 :(得分:0)
在AppDelegate中配置所有应用程序后,有两种方法可以帮助您完成整个代码(您可以在任何 viewController 中使用它)
返回先前使用给定名称创建的FIRApp实例,如果不存在此类应用,则返回nil。这种方法是线程安全的。
声明
class func app(name: String) -> FirebaseApp?
示例:
guard let primaryApp = FirebaseApp.app(name: "primaryOptions") else { return }
返回所有现存FIRApp实例的集合,如果没有FIRApp实例,则返回nil。这种方法是线程安全的。
声明
class func allApps() -> [String : FirebaseApp]?
示例:
guard let allApps = FirebaseApp.allApps, let primaryApp = allApps["primaryOptions"] else { return }
获取FirbaseApp实例后,您可以获得数据库参考
Database.database(app: primaryApp).reference()
答案 1 :(得分:0)
使用 Swift 4.2 可以正常工作,我正在为此发布解决方案。
如Firebase所述,它在AppDelegate中使用,但是如果您要对firstDB使用登录名或其他内容,建议您像我一样处理它。
在我的情况下,我通过FirstBD处理登录和身份验证,然后稍后从secondDB检索数据。
1-首先添加secondDB plist:
2-我创建了一个删除firstDB配置的函数,并配置了secondBD,如下所示:
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)
}
并在我想处理secondDB的ViewController viewDidLoad()中调用它:
override func viewDidLoad() {
configSecondDatabase()
let ref = Database.database().reference() // reference of secondDB
}
希望这会帮助某人,并节省他的时间。 谢谢
答案 2 :(得分:0)
如果您真的不需要在两个数据库之间切换,而只需要使用不同于默认数据库的新数据库,请转到GoogleService-Info.plist并将DATABASE_URL值更改为所需的Firebase数据库的URL使用。