Advertisements
NSBlockOperation by example
Managing the Blocks in the Operation
+ blockOperationWithBlock:
Creates and returns an NSBlockOperation object and adds the specified block to it.
Example
NSBlockOperation *eezyBlockOperation = [NSBlockOperation blockOperationWithBlock:^{ NSInteger result =0 ; for (NSInteger i=0; i<10000; i++) { result = i; } NSLog(@"Your operation block %d",result); }]; NSLog(@"Before add block "); NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init]; [operationQueue addOperation:eezyBlockOperation]; NSLog(@"After add block");
Output
2014-04-04 08:24:27.155 iOS-Tutorial[503:a0b] Before add block 2014-04-04 08:24:27.156 iOS-Tutorial[503:a0b] After add block 2014-04-04 08:24:27.156 iOS-Tutorial[503:1403] Your operation block 9999
- addExecutionBlock:
Adds the specified block to the receivers list of blocks to perform.
Example
NSBlockOperation *eezyBlockOperation = [NSBlockOperation blockOperationWithBlock:^{ NSInteger result =0 ; for (NSInteger i=0; i<10000; i++) { result = i; } NSLog(@"Your operation block %d",result); }]; [eezyBlockOperation addExecutionBlock:^{ NSLog(@"Added execution block "); }]; NSLog(@"Before add block "); NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init]; [operationQueue addOperation:eezyBlockOperation]; NSLog(@"After add block");
Output
2014-04-04 08:32:31.619 iOS-Tutorial[540:a0b] Before addd block 2014-04-04 08:32:31.620 iOS-Tutorial[540:a0b] After add block 2014-04-04 08:32:31.620 iOS-Tutorial[540:1403] Your operation block 9999 2014-04-04 08:32:31.620 iOS-Tutorial[540:3703] Added execution block
- executionBlocks
Returns an array containing the blocks associated with the receiver.
Example
NSBlockOperation *eezyBlockOperation = [NSBlockOperation blockOperationWithBlock:^{ NSInteger result =0 ; for (NSInteger i=0; i<10000; i++) { result = i; } NSLog(@"Your operation block %d",result); }]; [eezyBlockOperation addExecutionBlock:^{ NSLog(@"Added execution block "); }]; NSLog(@"%@",[eezyBlockOperation executionBlocks]);
Output
2014-04-04 08:35:45.239 iOS-Tutorial[570:a0b] ( "<__NSGlobalBlock__: 0x4080>", "<__NSGlobalBlock__: 0x40a8>" )
Advertisements