9

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.

Sharjeel Aziz
  • 8,495
  • 5
  • 38
  • 37
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143

2 Answers2

17

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...

Sharjeel Aziz
  • 8,495
  • 5
  • 38
  • 37
Zebs
  • 5,378
  • 2
  • 35
  • 49
  • but what happen if i have multiple operation queue? should i check it for every operation queue? i can do it but is this the best possible way? – Bryan Chen Mar 07 '11 at 08:54
  • 1
    @xlc0212: Another solution I can think of is to subclass NSOperation, which is actually encouraged by Apple; and add a Boolean property "isInQueue" that gets flagged to YES when you add it to a queue. Like this, you just need to check this property before adding operations to queues. – Zebs Mar 07 '11 at 09:08
  • when should i flag the operation? should i manually set it before i add it in the queue or should i override some method that will be invoked by the queue and then set the flag – Bryan Chen Mar 07 '11 at 09:31
  • The easiest way is to set it manually before you add it to the queque. Oo you could create a method inside your subclass called addToQueue: that tales a queue, flags the bool and then adds it to the queue you passed. – Zebs Mar 07 '11 at 22:52
  • 1
    Also, couldn't you just use `containsObject:` rather than iterating through the `operations` array? – paulbailey Mar 08 '11 at 17:53
  • @paulbailey: Very true! I have updated the answer to reflect your suggestion; (although under the hood the iteration still happens) it is cleaner code. – Zebs Mar 09 '11 at 04:41
4

When you add an NSOperation object to a NSOperationQueue, the NSOperationQueue retains the object, so the creator of the NSOperation can release it. If you keep with this strategy, NSOperationQueues will always be the only owner of their NSOperation objects, so you won't be able to add an NSOperation object to any other queue.

If you still want to reference individual NSOperation objects after they've been added to the queue, you can do so using the NSOperationQueue's - (NSArray *)operations method.

James Bedford
  • 28,702
  • 8
  • 57
  • 64