Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safe way to change the priority of an NSOperation

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.

like image 843
user130444 Avatar asked Jul 18 '09 00:07

user130444


People also ask

What is NSOperation and Nsoperationqueue in IOS?

Overview. An operation queue invokes its queued NSOperation objects based on their priority and readiness. After you add an operation to a queue, it remains in the queue until the operation finishes its task. You can't directly remove an operation from a queue after you add it. Note.

How do you pause NSOperation?

add a isPaused flag to your NSOperation subclass. implement a copy/move method for the operation's data. if paused, setCancelled: (watch for this change in -main ) create a new operation moving the state from paused operation to new operation.

What is the difference between GCD and Nsoperationqueue in IOS?

GCD is a low-level C API that enables developers to execute tasks concurrently. Operation queues, on the other hand, are high level abstraction of the queue model, and is built on top of GCD. That means you can execute tasks concurrently just like GCD, but in an object-oriented fashion.

What are NS operations?

An abstract class that represents the code and data associated with a single task.


1 Answers

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.

like image 118
Brad Larson Avatar answered Oct 04 '22 22:10

Brad Larson