.enforceQoS 标志如何增加优先级?

How does .enforceQoS flag increase priority?

我试图提高并发队列中执行的块的优先级。我建议标志 enforceQoS 适用于此目的。正如 Apple 文档所说:

This flag prioritizes the block's quality-of-service class over the one associated with the current execution context, as long as doing so does not lower the quality of service.

假设有一个队列:

/// Lowest priority queue
let concurrentQueue = DispatchQueue(label: "queue.conc.test", qos: .background, attributes: .concurrent)

我们还有几个 DispatchWorkItem 任务,其中一些是:

let item1 = DispatchWorkItem(qos: .userInitiated) { /* execute block */ }
let item2 = DispatchWorkItem(qos: .utility, flags: .enforceQoS) { /* execute block */ }

我的问题很简单:Apple 保证 item2 提前执行吗?

我在操场上玩了一下,可惜没有发挥出这个flag的优势,结果乱七八糟。官方文档在这件事上也没有帮助。

非常感谢。

这里的

"Prioritize"不是指"run sooner."而是指"when deciding which QoS to assign for this block, prefer the one given here you rather than the one that the current execution context suggests (if the one given here is higher than what this block would otherwise receive)."

如果您在 item1 之前需要 item2 运行,则使用 notify 来订购它们:

item2.notify(queue: concurrentQueue, execute: item1)

在任何情况下,您都不应尝试使用 QoS 来订购工作项。 QoS 是一个复杂的系统,不仅包括优先级;它还包括设备是否处于节能模式等内容。根据每个服务级别的描述,为项目分配符合其意图的 QoS。如果您需要按特定顺序 运行 的东西,请使用依赖项(如 notify)来对它们进行排序。