Created
July 3, 2016 17:24
-
-
Save loganwright/d4b893744d7114f78c6fcefb1c80f866 to your computer and use it in GitHub Desktop.
Semaphore
This file contains hidden or 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
[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