ajay
ajay

Reputation: 3345

NSOPeration canceled error

I am subclassing NSOperation in my current application. All functionalities are working well as per my desire. I am able to cancel operations and add new operations to my operation queue. Apart from the success, below is the warning/error statement I found in the console:

***SubOperation 0x19ec1bd0 went isFinished=YES without being started by the queue it is in*

By above warning I am able to find that, I am canceling operation before start. Because of this, Is there any memory leak or functionality break will happen in future?

What exactly is the intention of the warning?

So far functionality is working and there is no break on that, even still we need to consider the warning and resolve it?

All your suggestions are open.

Upvotes: 2

Views: 1321

Answers (2)

Neeku
Neeku

Reputation: 3653

You are responsible to check [self isCancelled] periodically while subclassing NSOperation and quit the operation if it is YES. The operation queue can not (instantly) cancel operations that are already running.

If you override ready, you must override cancel, too. What happens in the abstract class is that when cancel is called it sets the operation as ready so that the queue may call start, the start method checks for the canceled flag, then aborts the operation and sets isFinished=YES. Then the operation queue dealloc's the operation. You can't have one without the other.

Upvotes: 1

Ashwinkumar Mangrulkar
Ashwinkumar Mangrulkar

Reputation: 2965

then check it first the operation is in queue or not

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

NSOperation *operation = [[NSOperation alloc] init];

[queue addOperation:operation];

if([queue operations] containsObject:operation])
    NSLog(@"Operation is in the queue");
else
    NSLog(@"Operation is not in the queue");

Or you can iterate on all the objects:

for(NSOperation *op in [queue operations])
    if (op==operation) {
        NSLog(@"Operation is in the queue");
    }
    else {
        NSLog(@"Operation is not in the queue");
    }

Upvotes: 0

Related Questions