iOS - How to check if an NSOperation is in an NSOperationQueue?

11,946

Solution 1

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

Solution 2

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.

Share:
11,946
Bryan Chen
Author by

Bryan Chen

Updated on July 29, 2022

Comments

  • Bryan Chen
    Bryan Chen almost 2 years

    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.

  • Bryan Chen
    Bryan Chen about 13 years
    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?
  • Zebs
    Zebs about 13 years
    @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.
  • Bryan Chen
    Bryan Chen about 13 years
    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
  • Zebs
    Zebs about 13 years
    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.
  • paulbailey
    paulbailey about 13 years
    Also, couldn't you just use containsObject: rather than iterating through the operations array?
  • Zebs
    Zebs about 13 years
    @paulbailey: Very true! I have updated the answer to reflect your suggestion; (although under the hood the iteration still happens) it is cleaner code.