Last active
December 17, 2015 21:29
-
-
Save kimhunter/5674947 to your computer and use it in GitHub Desktop.
Operation Queue with in block cancellation
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
#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