Skip to content

Instantly share code, notes, and snippets.

@loganwright
Created July 3, 2016 17:24
Show Gist options
  • Save loganwright/d4b893744d7114f78c6fcefb1c80f866 to your computer and use it in GitHub Desktop.
Save loganwright/d4b893744d7114f78c6fcefb1c80f866 to your computer and use it in GitHub Desktop.
Semaphore
[queue addOperationWithBlock:^{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSURLSession *session = [NSURLSession sharedSession]; // or create your own session with your own NSURLSessionConfiguration
NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (data) {
// do whatever you want with the data here
} else {
NSLog(@"error = %@", error);
}
dispatch_semaphore_signal(semaphore);
}];
[task resume];
// but have the thread wait until the task is done
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
// now carry on with other stuff contingent upon what you did above
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment