ios - 'FIRAppNotConfigured',原因 : 'Failed to get default Firebase Database instance. Must call ` [FIRApp configure ]` (` FirebaseApp. configure()`(Swift 中)

标签 ios swift firebase firebase-realtime-database firebase-authentication

我有开发和生产 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/

相关文章:

ios - ViewController 通过在屏幕上交换转到上一个 ViewController

arrays - 检查 Swift 数组是否包含对象实例

ios - SpriteKit : Why does it wait one round for the score to update?( swift )

swift - setScreenname() 仅在 logEvent() 之后触发

firebase - 在将用户添加到 Firebase 时从 Go 管理后端获取消息 token ,在 Javascript 中注册时结束

iphone - UIActivityIndi​​catorView 卡住

ios - 具有大小类的自调整大小的 UITableViewCells 中的多行 UILabel - iOS 8 和 Swift

iphone - 想要在表中显示数组值

ios - UITableViewCell 内的 UICollectionView

Firebase:Firebase 中未显示事件属性数据