Firestore async issue

I'm calling a Firestore query that does come back, but I need to ensure completion before moving on with the rest of the code. So I need a completion handler...but for the life of me I can't seem to code it.

// get user info from db
    func getUser() async {
        self.db.collection("userSetting").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
                    let userTrust = document.data()["userTrust"] as! String
                    let userGrade = document.data()["userGrade"] as! String
                    let userDisclaimer = document.data()["userDisclaimer"] as! String
                    
                    var row = [String]()
                    row.append(userTrust)
                    row.append(userGrade)
                    row.append(userDisclaimer)
                    
                    self.userArray.append(row)
                    
                    // set google firebase analytics user info
                    self.userTrustInfo = userTrust
                    self.userGradeInfo = userGrade

                }
            }
            
        }
    }

Called by:

internal func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        FirebaseApp.configure()
        db = Firestore.firestore()
        Database.database().isPersistenceEnabled = true

        Task {
            do {
                let userInfo = await getUser()
            }
        } return true }

I used a Task as didFinishLauncingWithOptions is synchronous and not asynchronous

However, the getUser() still isn't completed before didFinishLauncingWithOptions moves on.

I need the data from getUser as the very next step uses the data in the array, and without it I get an 'out of bounds exception' as the array is still empty.

Also tried using dispatch group within the func getUser(). Again with no joy.

Finally tried a completion handler:

func getUser(completion: @escaping (Bool) -> Void) {

        self.db.collection("userSetting").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {

                for document in querySnapshot!.documents {

                    let userTrust = document.data()["userTrust"] as! String
                    let userGrade = document.data()["userGrade"] as! String
                    let userDisclaimer = document.data()["userDisclaimer"] as! String
                    
                    var row = [String]()
                    row.append(userTrust)
                    row.append(userGrade)
                    row.append(userDisclaimer)
                    
                    self.userArray.append(row)
                    
                    // set google firebase analytics user info
                    self.userTrustInfo = userTrust
                    self.userGradeInfo = userGrade
                    
                    completion(true)
                    }
                
                }
            }
        
        }

Nothing works. The getUser call isn't completed before the code moves on. Can someone please help. I've searched multiple times, looked at all linked answers but I can not make this work.I'm clearly missing something easy, please help



Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)