Skip to content

Instantly share code, notes, and snippets.

@andreacipriani
Created September 12, 2019 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreacipriani/50595fff40a0be640a4235f3c19de457 to your computer and use it in GitHub Desktop.
Save andreacipriani/50595fff40a0be640a4235f3c19de457 to your computer and use it in GitHub Desktop.
Dispatch group on Swift example: how to wait for multiple asynchronous closures to complete
let group = DispatchGroup()
group.notify(queue: .main, work: DispatchWorkItem(block: {
print("everything finished")
}))
func asyncOne() {
sleep(2)
group.enter()
print("finished A")
group.leave()
}
func asyncTwo() {
sleep(2)
group.enter()
print("finished B")
group.leave()
}
asyncOne()
asyncTwo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment