0

So, I am using the addOperationWithBlock^{}method of the NSOperationQueue and I want to put the fine grained isCancelled property check at points in the operation code. How do I get the instance of the operation that will run while in this block?

For example, this is my code snippet:

[operationQueueInstance addOperationWithBlock:^{
    if (!???.isCancelled){
        NSlog(@"Instruction 1");
    }
    if (!???.isCancelled){
        NSlog(@"Instruction 2");
    }

    NSlog(@"Instruction 3");
 }];

How do I get the instance of the operation this block would run in so that I can evaluate the isCancelled property for the instructions?

TIA

Liam George Betsworth
  • 18,373
  • 5
  • 39
  • 42
Anton Unt
  • 1,835
  • 1
  • 21
  • 47

1 Answers1

1

From the Apple docs:

You should not attempt to get a reference to the newly created operation object or divine its type information.

You could instantiate a NSBlockOperation and assign it to a variable to keep an explicit reference instead.

addOperationWithBlock seems to be a shortcut for whenever you don't need a reference to the operation object.

Make sure to mark your block operation with the __block storage qualifier to avoid retain cycles.

Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112