Skip to content

Instantly share code, notes, and snippets.

@standinga
Last active August 17, 2019 14:16
Show Gist options
  • Save standinga/2f78212a32f1a9cc0f99724b0ac87ae3 to your computer and use it in GitHub Desktop.
Save standinga/2f78212a32f1a9cc0f99724b0ac87ae3 to your computer and use it in GitHub Desktop.
fetchData function for medium blog post about DispatchGroup and DispatchSemaphore, https://link.medium.com/zJMQPUHaeZ
func combineAsyncCallsWithDispatchGroup(completionHandler: @escaping (String)->()) {
let group = DispatchGroup()
var text = ""
group.enter()
fetchData(0, delay: 0.4) {
text += $0
group.leave()
}
group.enter()
fetchData(1, delay: 0.2) {
text += $0
group.leave()
}
group.notify(queue: .main) {
completionHandler(text)
}
}
combineAsyncCallsWithDispatchGroup() {
print($0)
exit(0)
}
RunLoop.current.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment