Created
September 12, 2019 10:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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