在 applicationDidFinishLaunchingWithOptions 中获取 firestore 数据
fetching firestore data in applicationDidFinishLaunchingWithOptions
我正在使用 firebase firestore 和身份验证。
我的应用基本上是管理订单,当用户向 firestore 发送新订单时,它会得到一个 openOrder 默认布尔变量,我有另一个应用管理这个订单,一旦我的另一个应用读取订单,布尔变量就会改变值.
所有这些都有效。
我的问题是当用户完全关闭应用程序然后重新打开它时,我需要检查 openOrder 是否为真,并根据该设置设置我的 rootViewController 。
我正在使用完成处理程序来获取 openOrder var 并检查它是否为真但 applicationDidFinishLaunchingWithOptions returns 在我根据 firestore 函数设置本地变量之前为真。
我的代码是:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
let reValue = loadPriviousDishesIfUserQuitsAppBeforeClosingTab(completion: { success in
guard success! else { return }
//here I have all the values I need and need to return only here
})
return true
}
func loadPriviousDishesIfUserQuitsAppBeforeClosingTab(completion: @escaping (Bool?) ->()) {
db = Firestore.firestore()
let myUserID = Auth.auth().currentUser?.uid
db.collection("users").whereField("user", isEqualTo: myUserID!).whereField("openOrder", isEqualTo: true).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
completion(nil)
}
else {
for doc in (querySnapshot?.documents)! {
guard let restID = doc.data()[ResttId"]
else {return}
myRestaurant.restID = restID as? String
self.setMyRestMenu(completion: { success in
guard success else { return }
//here I set all my values using fetching all the data from firestore,
})
}
completion(true)
}
}
}
您可以在 rootViewController 上方显示加载 activity,直到获得该值,然后
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let reValue = loadPriviousDishesIfUserQuitsAppBeforeClosingTab(completion: { success in
guard success! else { return }
//here I have all the values I need and need to return only here
let stor = UIStoryboard.init(name: "Main", bundle: nil)
let welcomeView = stor.instantiateViewController(withIdentifier: "orderView")
let nav = UINavigationController(rootViewController: welcomeView )
nav.navigationBar.isHidden = true
self.window?.rootViewController = nav
})
return true
}
编辑:在此处设置故事板 ID
我正在使用 firebase firestore 和身份验证。
我的应用基本上是管理订单,当用户向 firestore 发送新订单时,它会得到一个 openOrder 默认布尔变量,我有另一个应用管理这个订单,一旦我的另一个应用读取订单,布尔变量就会改变值.
所有这些都有效。
我的问题是当用户完全关闭应用程序然后重新打开它时,我需要检查 openOrder 是否为真,并根据该设置设置我的 rootViewController 。 我正在使用完成处理程序来获取 openOrder var 并检查它是否为真但 applicationDidFinishLaunchingWithOptions returns 在我根据 firestore 函数设置本地变量之前为真。
我的代码是:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
let reValue = loadPriviousDishesIfUserQuitsAppBeforeClosingTab(completion: { success in
guard success! else { return }
//here I have all the values I need and need to return only here
})
return true
}
func loadPriviousDishesIfUserQuitsAppBeforeClosingTab(completion: @escaping (Bool?) ->()) {
db = Firestore.firestore()
let myUserID = Auth.auth().currentUser?.uid
db.collection("users").whereField("user", isEqualTo: myUserID!).whereField("openOrder", isEqualTo: true).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
completion(nil)
}
else {
for doc in (querySnapshot?.documents)! {
guard let restID = doc.data()[ResttId"]
else {return}
myRestaurant.restID = restID as? String
self.setMyRestMenu(completion: { success in
guard success else { return }
//here I set all my values using fetching all the data from firestore,
})
}
completion(true)
}
}
}
您可以在 rootViewController 上方显示加载 activity,直到获得该值,然后
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let reValue = loadPriviousDishesIfUserQuitsAppBeforeClosingTab(completion: { success in
guard success! else { return }
//here I have all the values I need and need to return only here
let stor = UIStoryboard.init(name: "Main", bundle: nil)
let welcomeView = stor.instantiateViewController(withIdentifier: "orderView")
let nav = UINavigationController(rootViewController: welcomeView )
nav.navigationBar.isHidden = true
self.window?.rootViewController = nav
})
return true
}
编辑:在此处设置故事板 ID