Skip to content

Instantly share code, notes, and snippets.

@arvindhsukumar
Last active April 7, 2016 15:23
Show Gist options
  • Save arvindhsukumar/a9caddebb21b73d0ec3bc606b3805612 to your computer and use it in GitHub Desktop.
Save arvindhsukumar/a9caddebb21b73d0ec3bc606b3805612 to your computer and use it in GitHub Desktop.
let dispatch_group: dispatch_group_t = dispatch_group_create()
func startRequests(){
asyncFunctionA()
asyncFunctionB()
dispatch_group_wait(dispatch_group, DISPATCH_TIME_FOREVER) // Wait forever! or set a reasonable default for timeout
}
func alsoStartRequests(){
asyncFunctionA()
asyncFunctionB()
dispatch_group_notify(dispatch_group, dispatch_get_main_queue()) {
// Single callback that is executed after all tasks are complete.
if (self.errorA != nil) || (self.errorB != nil) {
//Handle error
return
}
self.processResponse(self.responseA, self.responseB)
}
}
func asyncFunctionA(){
let urlRequest: NSURLRequest = ...
dispatch_group_enter(dispatch_group)
let task = self.session?.dataTaskWithRequest(urlRequest, completionHandler: {
(data:NSData?, response:NSURLResponse?, error:NSError?) in
self.errorA = error
self.responseA = response
dispatch_group_leave(self.dispatch_group)
})
}
func asyncFunctionB(){
let urlRequest: NSURLRequest = ...
dispatch_group_enter(dispatch_group)
let task = self.session?.dataTaskWithRequest(urlRequest, completionHandler: {
(data:NSData?, response:NSURLResponse?, error:NSError?) in
self.errorB = error
self.responseB = response
dispatch_group_leave(self.dispatch_group)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment