DispatchQueue Priorities

In attempting to create a high priority queue I ran into the problem of Apple’s terrible documentation on this front. We have this new DispatchQoS.QoSClass enumeration, which consists of { userInteractive, userInitiated, default, utility, background, and unspecified } with no explanation as to which values represent higher priorities (although one can make some guesses). I wrote the following to get some idea:

func printQueuePriorities() {
	var q = DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.high)
	Swift.print("High priority: \(q.qos)")
	q = DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.background)
	Swift.print("Background priority: \(q.qos)")
	q = DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default)
	Swift.print("Default priority: \(q.qos)")
	q = DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.low)
	Swift.print("Low priority: \(q.qos)")

The following is the output:

High priority: DispatchQoS(qosClass: Dispatch.DispatchQoS.QoSClass.userInitiated, relativePriority: 0)
Background priority: DispatchQoS(qosClass: Dispatch.DispatchQoS.QoSClass.background, relativePriority: 0)
Default priority: DispatchQoS(qosClass: Dispatch.DispatchQoS.QoSClass.default, relativePriority: 0)
Low priority: DispatchQoS(qosClass: Dispatch.DispatchQoS.QoSClass.utility, relativePriority: 0)

So I guess that means if you want a high priority queue, the correct QoSClass is userInitiated.

I tried to set the priority for a queue I created in a way I _thought_ was correct:

let globalQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated)
let queue = DispatchQueue(label: "Test Queue")
queue.setTarget(queue: globalQueue)

but queue.setTarget(queue: globalQueue) crashed, and the debugger gave me no clues as to why. So instead, I used the longer initializer for creating a queue:

let queue = DispatchQueue(label: "Test Queue", qos: .userInitiated,
						  attributes: DispatchQueue.Attributes(),
						  autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit,
						  target: globalQueue)

And although I haven’t tested this yet, at least it doesn’t crash.

One thought on “DispatchQueue Priorities
  1. JM says:



    The table 4-6 describes the mappings between the old high, default, low, and background global concurrent queues and their corresponding QoS classes.

Leave a Reply

Your email address will not be published. Required fields are marked *