Please consider the following statement:
DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + .milliseconds(500), qos: .utility, flags: .noQoS) {
print("What is my QOS?")
}
Notice how many of the parameters refer to the quality of service. How can a mere mortal possibility sort out the permutations?
Generally you shouldn't try to sort out all those permutations. In most cases, messing around too much with QoS is a recipe for trouble. But there are fairly simple rules.
Queues have priorities, and they can assign that priority to blocks that request to inherit.
This particular block is explicitly requesting a lower priority, but then says "ignore my QoS request." As a rule, don't do that. The only reason I know of for doing that is if you're interacting with some legacy API that doesn't understand QoS. (I've never encountered this myself, and it's hard to imagine it coming up in user-level code.)
A more interesting question IMO (and one that comes up much more often in real code) is this one:
DispatchQueue.global(qos: .utility).async(qos: .userInitiated) {}
What is the priority of this block? The answer is .userInitiated
, and the block will "loan" its priority to the queue until it finishes executing. So for some period of time, this entire queue will become .userInitiated
. This is to prevent priority inversion (where a high-priority task blocks waiting for a low-priority task).
This is all discussed in depth in Concurrent Programming With GCD in Swift 3, which is a must-watch for anyone interested in non-trivial GCD.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With