我有开发和生产 Firebase 项目,我正在使用以下方法进行管理,
在我的 AppDelegate didFinishLaunchingWithOptions 中,
// Configure with manual options.
let secondaryOptions = FirebaseOptions(googleAppID: "1:82121212:ios:212121212121212", gcmSenderID: "872909116998")
secondaryOptions.bundleID = "com.testApp.test"
secondaryOptions.apiKey = "AIzasskjkkjhkdhksadhsadaskdhks"
secondaryOptions.clientID = "82121212-asdsdasdasdasdasdadsdsd.apps.googleusercontent.com"
secondaryOptions.databaseURL = "https://test-myApp.firebaseio.com"
secondaryOptions.storageBucket = "test-myApp.appspot.com"
// Configure an alternative FIRApp.
FirebaseApp.configure(name: "secondary", options: secondaryOptions)
guard let secondary = FirebaseApp.app(name: "secondary")
else { assert(false, "Could not retrieve secondary app") }
let secondaryDb = Database.database(app: secondary)
在我的另一个 View Controller 中,当从 firebase 数据库获取消息时,我的应用程序崩溃了
“*** 由于未捕获的异常“FIRAppNotConfigured”而终止应用,原因:“无法获取默认 Firebase 数据库实例。必须调用 [FIRApp 配置]
(FirebaseApp.configure()
在 Swift 中)在使用 Firebase 数据库之前。'"
我的ChatViewController
class ChatViewController : UIViewController {
var ref : DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
getChatMessages()
}
@objc func getChatMessages() {
firebase = Database.database().reference(fromURL: url)
Auth.auth().signIn(withCustomToken: firBaseAuthToken, completion: { (user, error) in
if error != nil{
}
else
{
let userAuthData = user?.additionalUserInfo
self.uid = (user?.user.uid)!.replacingOccurrences(of: ".", with: "")
self.successFireBaseAuth(urlString: klUrl)
}
})
}
func successFireBaseAuth(urlString : String)
{
ref = Database.database().reference(withPath: urlString)
ref.observe(.value, with: { snapshot in
})
}
最佳答案
从外观上看,您正在访问错误的应用程序实例。
guard let secondary = FirebaseApp.app(name: "secondary")
else { assert(false, "Could not retrieve secondary app") }
let secondaryDb = Database.database(app: secondary)
您自己在 AppDelegate 中的代码,我假设这就是您应该如何获取正确的数据库实例。
但你正在使用
Database.database()
尝试获取消息时。
如果您想使用共享实例,请使用
FirebaseApp.configure(options: secondaryOptions)
配置时
关于ios - 'FIRAppNotConfigured',原因 : 'Failed to get default Firebase Database instance. Must call ` [FIRApp configure ]` (` FirebaseApp. configure()`(Swift 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58782401/