Skip to content

Instantly share code, notes, and snippets.

@LovaRK
Created February 7, 2022 14:55
Show Gist options
  • Save LovaRK/a8c5ede17dfa729287d3011fbd10ec8c to your computer and use it in GitHub Desktop.
Save LovaRK/a8c5ede17dfa729287d3011fbd10ec8c to your computer and use it in GitHub Desktop.
How to do two concurrent API calls in swift
func downloadDetails(){
let dispatchGroup = DispatchGroup()
dispatchGroup.enter() // <<---
WebServiceManager.getAData(format:A, withCompletion: {(data: Any? , error: Error?) -> Void in
if let success = data {
DispatchQueue.main.async {
(success code)
dispatchGroup.leave() // <<----
}
}
})
dispatchGroup.enter() // <<---
webServiceManager.getBData(format: B, withCompletion: {(data: Any? , error: Error?) -> Void in
if let success = data {
DispatchQueue.main.async {
(success code)
dispatchGroup.leave() // <<----
}
}
})
dispatchGroup.notify(queue: .main) {
// whatever you want to do when both are done
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment