H.Epstein
H.Epstein

Reputation: 731

fetching firestore data in applicationDidFinishLaunchingWithOptions

I'm using firebase firestore and authentication .

My app is basically managing orders, when a user sends a new order to firestore it gets a openOrder default Boolean var, I have another app that manage this order and once my other app reads the order the boolean var changes value.

All of that works.

My issue is when a user closes completly the app and then reopens it I need to check if the openOrder is true or not and according to that set my rootViewController . I'm using a completion handler to fetch the openOrder var and check if it is true or not but applicationDidFinishLaunchingWithOptions returns true before I set my local vars according to the firestore functions.

my code is :

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

}

Upvotes: 0

Views: 124

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100523

You can show a loading activity above the rootViewController until get that value , then

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
}

Edit : set storyboardID here

enter image description here

Upvotes: 1

Related Questions