ios - 火力地堡错误 : Calls to setPersistenceEnabled must be made before any other usage of FIRDatabase instance

标签 ios swift firebase firebase-realtime-database

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

来 self 的 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 吗?

最佳答案

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

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

关于ios - 火力地堡错误 : Calls to setPersistenceEnabled must be made before any other usage of FIRDatabase instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51641510/

相关文章:

arrays - 我如何确保用户不能重复输入?

swift - 如何通过 NSDate 对 Swift 中的字典进行排序?

java - 错误 Gradle 同步失败 : For input string: "12" when apply plugin: 'com.google.gms.google-services'

firebase - Flutter Firebase Cloud Messaging 消息文本和标题

swift - Watson VisualRecognition 无法对图像进行分类,报错 400(使用 swift)

卸载应用程序时 Firebase 删除用户

ios - 什么 View 对象是 Apple 日历应用程序 (iOS/OS X) 的基础

ios - 在 UITextView 上以编程方式禁用 iOS8 Quicktype 键盘

ios - 在 Firestore 中获取数据

ios - 标签栏 Controller : Show Login Controller + Stop Double Tap on Tab Button