I have an async function with a completionHandler
(let's say it's called asyncFunction
) that I'm calling inside another function. I want this second function to wait for the async function. So, basically I want this second function to be run synchronously. I'm using DispatchGroup
and wait()
, but it looks like there's a deadlock. It waits forever.
Here's what I'm doing.
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
DispatchQueue.global().async {
self.asyncFunction() { error in
if let error = error {
debugPrint(error.alertText)
}
dispatchGroup.leave()
}
}
dispatchGroup.wait()
What am I doing wrong?
By the way, I can't use notify()
in this case. I don't want my function to have a completion handler.