NSOperationQueue
类有一个 underlyingQueue
属性,用于设置将在其上执行 NSOperation
实例的调度队列。
let dispatchQueue = dispatch_queue_create("custom.queue", DISPATCH_QUEUE_SERIAL)
let opQueue = NSOperationQueue()
opQueue.underlyingQueue = dispatchQueue
但是,官方文档指出:
The value of this property must not be the value returned by
dispatch_get_main_queue
Apple 似乎没有对这个问题做更多的解释。但是,将主队列用作 underlyingQueue
不会引发任何错误,也不会产生任何不需要的行为。这背后的原因是什么?
最佳答案
可能有多种原因;死锁的可能性会在脑海中浮现,但您需要向 Apple 寻求明确的答案。
如果您的整个操作必须在主队列上执行,那么您可以使用 NSOperationQueue.mainQueue
获取与主线程关联的操作队列。如果只有部分操作需要在主队列上执行(例如更新 UI 元素),那么您可以根据需要将该操作同步或异步地分派(dispatch)到主队列。
关于ios - 将主队列设置为 NSOperationQueue 的底层队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39068798/