【发布时间】: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