在applicationDidFinishLaunchingWithOptions中获取firestore数据

时间:2018-03-20 20:37:52

标签: swift google-cloud-firestore completionhandler

我正在使用firebase firestore和身份验证。

我的应用程序基本上是管理订单,当用户向firestore发送新订单时,它获得openOrder默认的布尔值var,我有另一个应用程序管理此订单,一旦我的其他应用程序读取布尔值更改值的顺序。

所有这些都有效。

我的问题是,当用户完全关闭应用程序然后重新打开它时,我需要检查openOrder是否为真,并根据设置我的rootViewController。 我使用完成处理程序来获取openOrder var并检查它是否为true,但在我根据firestore函数设置本地变量之前,applicationDidFinishLaunchingWithOptions返回true。

我的代码是:

   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)
        }
    }

}

1 个答案:

答案 0 :(得分:1)

您可以在rootViewController上方显示加载活动,直到获得该值,然后

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
}

编辑:在这里设置storyboardID

enter image description here