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)
As you can see, the QoS for the thread that is running the code is precisely what you would expect it to be.
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)
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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…