【问题标题】:'FIRAppNotConfigured', reason: 'Failed to get default Firebase Database instance. Must call `[FIRApp configure]` (`FirebaseApp.configure()` in Swift)'FIRAppNotConfigured',原因:'未能获取默认 Firebase 数据库实例。必须调用`[FIRApp configure]`(Swift 中的`FirebaseApp.configure()`)
【发布时间】:2020-03-06 00:30:53
【问题描述】:

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

在我的另一个视图控制器中,从 firebase 数据库获取消息时,我的应用程序崩溃了 "*** 由于未捕获的异常 'FIRAppNotConfigured' 导致应用程序终止,原因:'无法获取默认 Firebase 数据库实例。在使用 Firebase 数据库之前必须调用 [FIRApp configure](在 Swift 中为 FirebaseApp.configure())。'"

我的 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
 })
}

【问题讨论】:

  • 看起来您输入的应用程序名称错误。
  • 不,我确认了,并且在 Firebase 仪表板中的名称与 Applegate 中提到的名称相同。
  • PROJECT_ID , REVERSED_CLIENT_ID 也丢失了,只需从 firebase 下载 plist 并计算每个参数。

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


【解决方案1】:

从外观上看,您访问的是错误的应用实例。

    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)

配置时

【讨论】:

  • 但是应该如何发现我得到了正确的数据库实例?
  • 好吧,如果您在配置 firebase 应用程序时为其命名,则无法从共享实例(即:database())访问它,因此您应该调用正确的 firebase.app,如 sn-p 中所述我的评论,如果你想配置共享实例,你应该删除名称并使用 configure(options: yourOptions) 进行初始化
猜你喜欢
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
相关资源
最近更新 更多