Created
January 12, 2013 22:18
-
-
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.
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
__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