Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set priority to an NSOperationQueue

GCD enables dispatching blocks to a queue according to 4 global priority queues (high, default, low, background). I have several NSOperationQueues in my app but want to run each in different priority. As i understood NSOperationQueue is an abstraction over GCD, and would like to set different priorities to the NSOperationQueue (similar to the GCD priority queues). Is there a way to do so? (found its possible to set a priority to an operation but not to the queue itself).

like image 736
user2261801 Avatar asked Apr 09 '13 16:04

user2261801


People also ask

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

What is NS operation?

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

What is difference between dispatch queue and NSOperationQueue?

NSOperationQueue can be more suitable for long-running operations that may need to be cancelled or have complex dependencies. GCD dispatch queues are better for short tasks that should have minimum performance and memory overhead.

What is the difference between NSOperationQueue and GCD?

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.


2 Answers

Starting with iOS 8, the NSOperationQueue has a qualityOfService property, which does what I think the OP meant. From the Class Reference:

This property specifies the service level applied to operation objects added to the queue. If the operation object has an explicit service level set, that value is used instead. [...]

Service levels affect the priority with which operation objects are given access to system resources such as CPU time, network resources, disk resources, and so on. Operations with a higher quality of service level are given greater priority over system resources so that they may perform their task more quickly. You use service levels to ensure that operations responding to explicit user requests are given priority over less critical work.

Also in iOS 8, you can change which GCD queue is used with the underlyingQueue property, and therefore choose the global GCD queue with the desired priority.

like image 51
fishinear Avatar answered Nov 06 '22 22:11

fishinear


A quote from NSOperationQueue Class Reference.

Operations within the queue (but not yet executing) are themselves organized according to priority levels and inter-operation object dependencies and are executed accordingly.

Inter-operation dependencies provide an absolute execution order for operations, even if those operations are located in different operation queues. An operation object is not considered ready to execute until all of its dependent operations have finished executing. For operations that are ready to execute, the operation queue always executes the one with the highest priority relative to the other ready operations.

Which means by design, you cannot set priority to a queue, but NSOperationQueue will use GCD global queue for individual operations depending on the operation's individual priority level.

like image 34
svena Avatar answered Nov 06 '22 22:11

svena