TomSwift
TomSwift

Reputation: 39512

how to cancel out of operation created with addOperationWithBlock?

I'm using NSOperationQueue's addOperationWithBlock. From within the block, how do I check to see if I'm supposed to cancel the operation? Or access any NSOperation properties/methods?

[myOperationQueue addOperationWithBlock: ^{

  while ( /* long running loop */ )
  {
      // how to determine here if I need to cancel?
      // for that matter, access any NSOperation properties/methods?

  }

}];

Is the better way to do this to use a NSBlockOperation?

Upvotes: 18

Views: 7017

Answers (2)

jemmons
jemmons

Reputation: 18657

A better solution might be to use NSBlockOperation and add that to the queue instead of a raw block. You could do something like:

__block NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  while(![operation isCancelled]){
    //Some long operation
  }
}];

[[self queue] addOperation:operation];

This lets you use blocks while giving you a little more control over the operation... and a few more NSOperation niceties as well (like the ability to add completion blocks, for example).

Upvotes: 51

Joshua Smith
Joshua Smith

Reputation: 6621

You can't really check to see if you need to cancel the operation if it's in a block. If it's in a block and it's supposed to be canceled then it is canceled. Accessing NSOperation properties is not possible because the block is not an NSOperation instance per se.

Example code:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSOperationQueue *q = [[NSOperationQueue alloc] init];
    [q addOperationWithBlock:^{
        [NSThread sleepForTimeInterval:10];
        NSLog(@"Block 1");
    }];
    [q addOperationWithBlock:^{
        [NSThread sleepForTimeInterval:3];
        NSLog(@"Block 2");
    }];
    [q cancelAllOperations];
    [NSThread sleepForTimeInterval:15];

    [pool drain];
    return 0;
}

If you remove the cancelAllOperations call then the blocks fire as you would expect.

I would suggest that if you need to have finer grained control over the operation's cancel state and interplay with the NSOperationQueue that you would be better off using an NSOperation rather than an NSBlockOperation. You can subclass NSOperation to accomplish this.

Upvotes: 2

Related Questions