Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel NSBlockOperation

I have a long running loop I want to run in the background with an NSOperation. I'd like to use a block:

NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{    while(/* not canceled*/){       //do something...    } }]; 

The question is, how to I check to see if it's canceled. The block doesn't take any arguments, and operation is nil at the time it's captured by the block. Is there no way to cancel block operations?

like image 565
jemmons Avatar asked Nov 13 '11 17:11

jemmons


2 Answers

Doh. Dear future googlers: of course operation is nil when copied by the block, but it doesn't have to be copied. It can be qualified with __block like so:

//THIS MIGHT LEAK! See the update below. __block NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{    while( ! [operation isCancelled]){       //do something...    } }]; 

UPDATE:

Upon further meditation, it occurs to me that this will create a retain cycle under ARC. In ARC, I believe __block storage is retained. If so, we're in trouble, because NSBlockOperation also keeps a strong references to the passed in block, which now has a strong reference to the operation, which has a strong reference to the passed in block, which…

It's a little less elegant, but using an explicit weak reference should break the cycle:

NSBlockOperation *operation = [[NSBlockOperation alloc] init]; __weak NSBlockOperation *weakOperation = operation; [operation addExecutionBlock:^{    while( ! [weakOperation isCancelled]){       //do something...    } }]; 

Anyone that has ideas for a more elegant solution, please comment!

like image 80
jemmons Avatar answered Oct 06 '22 10:10

jemmons


To reinforce jemmons answer. WWDC 2012 session 211 - Building Concurent User Interfaces (33 mins in)

NSOperationQueue* myQueue = [[NSOperationQueue alloc] init]; NSBlockOperation* myOp = [[NSBlockOperation alloc] init];  // Make a weak reference to avoid a retain cycle __weak NSBlockOperation* myWeakOp = myOp;  [myOp addExecutionBlock:^{     for (int i = 0; i < 10000; i++) {         if ([myWeakOp isCancelled]) break;         precessData(i);     } }]; [myQueue addOperation:myOp]; 
like image 44
Robert Avatar answered Oct 06 '22 08:10

Robert