Skip to content

Instantly share code, notes, and snippets.

@ksm
Created September 10, 2012 07:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksm/3689424 to your computer and use it in GitHub Desktop.
Save ksm/3689424 to your computer and use it in GitHub Desktop.
NSOperation cancel pattern and retain cycle avoidance
/*
Source: Building Concurrent User Interfaces on iOS
WWDC2012 Session 211 by Andy Matuschak
*/
NSOperationQueue *queue = [[NSOperation alloc] init];
NSBlockOperation *op = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakOp = op;
[op addExecutionBlock:^{
for (int i = 0; i < 10000; i++) {
if ([weakOp isCancelled]) break;
processData(data[i]);
}
}];
[queue addOperation:op];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment