I am relatively new to async functions and I understand that the firebase getDocument and getDocuments calls are async. I would like both of these calls to finish before I move on to what I was doing in the code. I've been trying to implement this with dispatch groups, but have been unsuccessful thus far. I have code like the following:
let myGroup = DispatchGroup()
self.errorMessage = ""
let usersRef = self.db.collection("Users").document("Users").collection("Users")
if self.test == false {
self.errorMessage = "test failed"
} else{
//first async call
myGroup.enter()
usersRef.getDocuments {(snap, err) in
//basically getting every username
for document in snap!.documents{
print("loop")
let user = document["username"] as! String
let userRef = usersRef.document(user)
//second async call
userRef.getDocument { (snapshot, err) in
if err != nil {
print(err)
} else {
let self.error = snapshot!["error"] as! Bool
if self.error == true{
self.errorMessage = "error"
print("error")
}
print("what3")
}
print("what2")
}
print("what1")
}
myGroup.leave()
print("what4")
}
//RIGHT HERE I WANT TO CONTINUE WHAT I WAS DOING BEFORE
myGroup.notify(queue: DispatchQueue.global(qos: .background)) {
print("HERE I SHOULD BE DONE")
}
print("what5")
}
However, this produces something like:
what5
loop
what1
loop
what1
loop
what1
loop
what1
loop
what1
loop
what1
what4
HERE I SHOULD BE DONE
error
what3
what2
error
what3
what2
what3
what2
error
what3
what2
what3
what2
error
what3
what2
So it seems like the FIRST async call is finishing, but then the second continues executing. I'd like to wait for the second to finish before continuing.
Any advice on how to modify this code would be greatly appreciated. Thanks.