views:

239

answers:

3

Is there a safe way to change the priority of an NSOperation that is already enqueued in an NSOperationQueue? If the operation hasn't started yet there is no problem, but if it is already executing I get an NSInvalidArgumentException.

A: 

Try to set the priority, and catch the exception.

Peter Hosey
I tried that but for some reason the code in the @catch block never gets executed.
+2  A: 

You're not supposed to alter an NSOperation once it has been enqueued. According to Apple's Threading Programming Guide:

Important: You should never modify an operation object after it has been added to a queue. While waiting in a queue, the operation could execute at any time. Changing its status while it is executing could have adverse effects. You can use the methods of the operation object to determine if the operation is running, waiting to run, or already finished.

Brad Larson
A: 

On the docs there is no warning about not changing the priority after the operation has started and there is no mention about raised exceptions.

In fact changing the priority of a running operation simply has no effect. Maybe you're making something wrong elsewhere?

IlDan