Yuwen Yan
Yuwen Yan

Reputation: 4935

How to execute a function before an NSOperation is cancelled in NSOperationQueue

I want to execute a function before an NSOperation is cancelled. In main function, I add below code to achieve this goal:

if (self.isCancelled) {
    [self doSomething];
    return;
}

But if I cancel an operation before its start method is called, where should I call doSomething?

For queued operations, it simply marks the operation as ready to execute and lets the queue call its start method, which subsequently exits and results in the clearing of the operation from the queue.

According to above Apple's document, I know that I can call doSomething in start function, so am I right?

- (void)start {
    if (self.isCancelled) {
        [self doSomething];
    }
    [super start];
}

Upvotes: 0

Views: 215

Answers (1)

Chris
Chris

Reputation: 2747

I would set the code you want to run in the completionblock.

Upvotes: 1

Related Questions