本文介绍了对Operation和OperationQueue QoS关系感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

qualityOfService 属性的 OperationQueue 文档状态:

This property specifies the service level applied to operation
objects added to the queue

不过,只需复制& ;;将下面的代码粘贴到新的游乐场中.

However one can simply check that this is not true by just copy & pasting the code below into a new playground.

import Foundation

let q = OperationQueue()
q.qualityOfService = .userInitiated

print("QUEUE", q.qualityOfService.rawValue)

let op = BlockOperation()
op.addExecutionBlock {
    print("OP", op.qualityOfService.rawValue)
}

q.addOperations([op], waitUntilFinished: true)

您将看到队列QoS级别为 25 / .userInitiated 和操作 -1 / .default 代码>.

You will see that the queues QoS level is 25/.userInitiated and the operations -1/.default.

因此,这是否意味着操作QoS级别相对于队列 .userInitiated 级别是 .default ,还是 .default ,尽管队列具有更高的QoS级别?

So does it mean that the operations QoS level is .default in relation to the queues elevanted .userInitiated level, or is it .default, despite the queue having a higher QoS level?

我实际上期望的是这两个值应该相同.

What I actually expect is that these 2 values should be the same.

PS:我需要在 Operation 中调用一个 Process ,该进程又具有一个 qualityOfService 设置,该设置应与队列/操作.

PS: I need to invoke a Process inside the Operation, which in turn also has a qualityOfService setting that should be the same as the queue/ops.

推荐答案

操作的 qualityOfService 指示操作本身是否需要规定特定的QoS.但是 -1 / .default 有效地意味着它将仅对队列使用QoS(并因此使用所使用的工作线程的QoS).我不会非常担心操作的QoS.您关心的是运行它的线程的QoS:

The operation’s qualityOfService indicates whether the operation, itself, needs to dictate a particular QoS. But -1/.default effectively means that it will just use the QoS for the queue (and thus that of the worker thread that is used). I would not be terribly concerned about the QoS of the operation. What you care about is the QoS of the thread on which it runs:

let q = OperationQueue()
q.qualityOfService = .userInitiated

print("CURRENT", Thread.current.qualityOfService.rawValue)     // CURRENT 33
print("QUEUE", q.qualityOfService.rawValue)                    // QUEUE 25

let op = BlockOperation {
    print("THREAD", Thread.current.qualityOfService.rawValue)  // THREAD 25
}

q.addOperations([op], waitUntilFinished: false)

如您所见,运行代码的线程的QoS正是您所期望的.

As you can see, the QoS for the thread that is running the code is precisely what you would expect it to be.

如果需要,您可以看到将操作的QoS更改为高于队列的值将如何影响运行它的工作线程的QoS.因此,没有为此操作指定QoS的后台QoS队列:

If you want, you can see how changing the operation’s QoS to something higher than the queue will affect the QoS of the worker thread upon which it runs. Thus, background QoS queue with no QoS specified for the operation:

let q = OperationQueue()
q.qualityOfService = .background

print("CURRENT", Thread.current.qualityOfService.rawValue)    // CURRENT 33
print("QUEUE", q.qualityOfService.rawValue)                   // QUEUE 9

let op = BlockOperation()
op.addExecutionBlock {
    print("OP", op.qualityOfService.rawValue)                 // OP -1
    print("THREAD", Thread.current.qualityOfService.rawValue) // THREAD 9
}

q.addOperations([op], waitUntilFinished: false)

但是,您可以根据需要为操作指定特定的QoS,在这种情况下,可以将其升级为更高的QoS:

But you can, if you want, specify a particular QoS for the operation, in this case escalating it to a higher QoS:

let q = OperationQueue()
q.qualityOfService = .background

print("CURRENT", Thread.current.qualityOfService.rawValue)    // CURRENT 33
print("QUEUE", q.qualityOfService.rawValue)                   // QUEUE 9

let op = BlockOperation()
op.qualityOfService = .userInitiated                          // change op’s QoS, and thus the worker thread to a higher QoS, too
op.addExecutionBlock {
    print("OP", op.qualityOfService.rawValue)                 // OP 25
    print("THREAD", Thread.current.qualityOfService.rawValue) // THREAD 25
}

q.addOperations([op], waitUntilFinished: false)

这篇关于对Operation和OperationQueue QoS关系感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 13:26