swiftgrand-central-dispatchqos

How does quality of service (QOS) change when a dispatchWorkItem of higher QOS is added to a dispatchQueue of lower QOS?


I've found a documentation about how QOS get promoted for operations and operations queues: https://developer.apple.com/library/archive/documentation/Performance/Conceptual/EnergyGuide-iOS/PrioritizeWorkWithQoS.html

But haven't found anything about dispatchQueues. There are three ways QOS is assigned:

  1. to the DispatchQueue itself

  2. When we dispatch a task to a dispatchQueue: func async(group: DispatchGroup?, qos: DispatchQoS, flags : DispatchWorkItemFlags, execute: () -> Void)

  3. We can assign a QOS to a dispatchWorkItem itself

I was wondering how the QOS is promoted when these QOS are different.


Solution

  • You’ve outlined three ways to specify Quality of Service (QoS). But the latter two are really the same, just one is using blocks and the other DispatchWorkItem. So let’s focus on these two QoS dimensions, that of the queue and that of the dispatched task.

    In short, the queue QoS takes precedence over the dispatch item’s QoS unless either:

    Frankly, most of the time, we just set the queue QoS and call it a day. It is clear, concise, and makes it very easy to reason about our code. Specifying QoS at the task level can be done, but is less common. And specifying QoS for both queue and the dispatched items and overriding the QoS with the .enforceQoS flag is very unusual.

    For more information about QoS in general, see WWDC 2015 Building Responsive and Efficient Apps with GCD and WWDC 2016 Concurrent Programming With GCD in Swift 3. The first video introduces us to QoS (and, as you can see, when they introduced this topic, the focus was within dispatch queues), but this video admittedly uses the old Swift 2 syntax, although the concepts are unchanged. The second video introduces us to the contemporary Swift syntax and briefly recaps many of the discussions of the prior video.

    There are all sorts of interesting (and unusual) edge cases (e.g. GCD’s handling of priority inversions), but that’s discussed in the above videos and is beyond the scope of this question.