我正在尝试使用我的 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/