ios - 将主队列设置为 NSOperationQueue 的底层队列

标签 ios swift nsoperation nsoperationqueue

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/

相关文章:

ios - 循环数组并重新加载数据的更好方法?

ios - 从远程通知启动时如何使用导航堆栈呈现适当的 View Controller

iphone - 单元测试 NSOperation?

ios - Xcode Assets 目录 : Only one copy of each image

iphone - 从 iOS 应用程序调用

ios - 试图获取视频缩略图

ios - 隐藏 PayPal MPL 的 UiAlert

ios - 当遇到 subview (UIStackView > ContainerView)时,UIScrollView 停止垂直滚动

objective-c - NSOperation 依赖和 completionBlock

ios - 并发操作、NSOperationQueue 和异步 API 所需的说明