Skip to content

Instantly share code, notes, and snippets.

@kimhunter
Last active December 17, 2015 21:29
Show Gist options
  • Save kimhunter/5674947 to your computer and use it in GitHub Desktop.
Save kimhunter/5674947 to your computer and use it in GitHub Desktop.
Operation Queue with in block cancellation
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool
{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;
__block NSBlockOperation *operation = [[NSBlockOperation alloc] init];
[operation addExecutionBlock:^{
NSLog(@"block operation started");
for (int i = 10; i > 0; --i)
{
if (operation.isCancelled)
{
NSLog(@"block operation cancelled");
return;
}
sleep(2);
NSLog(@"block running %d", i);
}
NSLog(@"block operation ended");
}];
[queue addOperation:operation];
sleep(8);
NSLog(@"Cancelling all operations");
[queue cancelAllOperations];
[queue waitUntilAllOperationsAreFinished];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment