Skip to content

Instantly share code, notes, and snippets.

@eralston
Created February 27, 2014 18:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save eralston/9256577 to your computer and use it in GitHub Desktop.
A contrived asynchronous method using two operation queues
-(void)exampleAsyncMethod
{
// This method can have a dedicated queue for all instances on it
// Or maybe per instance if downloadQueue were instead a class variable
static NSOperationQueue *downloadQueue = nil;
if(!downloadQueue) {
downloadQueue = [[NSOperationQueue alloc] init];
}
// Capture the queue that called us
NSOperationQueue* callbackQueue = [NSOperationQueue currentQueue];
if(!callbackQueue) {
callbackQueue = [NSOperationQueue mainQueue];
}
// Do any setup work here
// Use the download queue to get the image, then apply it on the main queue
[downloadQueue addOperationWithBlock:^{
// So some work on the other thread, capturing work back into self
// Or into captured field in the block below
[callbackQueue addOperationWithBlock:^{
// Do some finalizing work back on the calling thread here
// You can use values you captured into this block or perhaps class variables
}];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment