Skip to content

Instantly share code, notes, and snippets.

@shepting
Created January 12, 2013 22:18
Show Gist options
  • Save shepting/4520739 to your computer and use it in GitHub Desktop.
Save shepting/4520739 to your computer and use it in GitHub Desktop.
Use a captured variable to allow a block to be cancelled in the midst of a long-running process. This is essentially the same as the NSOperation isCancelled flag.
__block BOOL isCancelled = NO;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
for (int i = 0; i < 10; i++) {
NSLog(@"Loop %i", i);
if (isCancelled) {
break;
} else {
sleep(1);
}
}
});
sleep(3);
isCancelled = YES;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment