A contrived asynchronous method using two operation queues
This file contains 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
-(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