6

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

NobodyNada
  • 7,529
  • 6
  • 44
  • 51
user2261801
  • 61
  • 1
  • 2

3 Answers3

5

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.

fishinear
  • 6,101
  • 3
  • 36
  • 84
1

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.

svena
  • 2,769
  • 20
  • 25
  • no, NSOperationQueue only uses one GCD global queue, the default priority one. The NSOperation priority concept is unrelated. – das Apr 10 '13 at 02:09
  • I did not claim it uses GCD global queue with different priority levels. What I said and meant to say is that it executes tasks in order that depends on operation's individual priority level. @das – svena Apr 10 '13 at 05:05
  • ok, your mention of 'GCD global queues' (plural) led me to that impression – das Apr 10 '13 at 06:10
-2

Try setting threadPriority on the NSOperations that you add to the queue.

Chetan
  • 46,743
  • 31
  • 106
  • 145