【问题标题】:Firebase error: Calls to setPersistenceEnabled must be made before any other usage of FIRDatabase instanceFirebase 错误:必须在使用 FIRDatabase 实例之前调用 setPersistenceEnabled
【发布时间】:2018-08-01 20:06:39
【问题描述】:

我正在尝试使用我的 firebase 数据库启用离线数据。不幸的是,在运行此代码时,它告诉我“必须在使用任何其他 FIRDatabase 实例之前调用 setPersistenceEnabled。”我相信这是因为 firebase 调用是异步的,但我不知道如何解决这个问题。

来自我的 AppDelegate:

        //Firebase database
        let myDatabase = Database.database().reference()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        //Firebase
        FirebaseApp.configure()
        Database.database().isPersistenceEnabled = true

        //check if user is already logged in
        if Auth.auth().currentUser != nil {

            let storyboard = UIStoryboard(name: "Main", bundle: nil)

            self.window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "signedIn")

        } else {
            // No user is signed in. Directs to Login/Register view.
        }

        return true
    }

我已经尝试注释掉检查用户登录部分,以防 Auth.auth() 搞砸了,但它没有修复它。我应该将其中一些东西放在其他地方而不是我的 AppDelegate 吗?

【问题讨论】:

  • 出于好奇,为什么代码中包含let myDatabase = Database.database().reference()这一行?好像myDatabase 没有被使用。
  • 我在其他地方使用它。我在粘贴之前不小心把它放在那里了。它应该在 AppDelegate 的顶部声明。
  • 您应该从 AppDelegate 中删除“let myDatabase = Database.database().reference()”这一行。“Database.database().isPersistenceEnabled = true”应该是在访问数据库之前首先执行的正如错误所说。

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

您在初始化 Firebase 之前调用了数据库。从 AppDelegate 中删除 let myDatabase 并在 ViewControllers 中使用它们。

您的错误与持久性无关,而是由在您的 AppDelegate 中设置之前调用 Firebase 数据库引起的。

【讨论】:

    猜你喜欢
    • 2016-10-11
    • 2016-11-05
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 2021-10-07
    • 2021-10-09
    • 2021-11-01
    相关资源
    最近更新 更多