From the docs:
An operation object can be in at most one operation queue at a time and this method throws an NSInvalidArgumentException exception if the operation is already in another queue. Similarly, this method throws an NSInvalidArgumentException exception if the operation is currently executing or has already finished executing.
So how do I check if I can safely add an NSOperation into a queue?
The only way I know is add the operation and then try to catch the exception if the operation is already in a queue or executed before.
NSOperationQueue
objects have a property called operations
.
If you have a reference to you queues it is easy to check.
You can check if the NSArray of operations contains your NSOperation
like this:
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");
}
Tell me if this is what you are looking for.
Alternatively, NSOperation
objects have several properties that allow you to check their state; such as: isExecuting
, isFinished
, isCancelled
, etc...