我很好奇这两种将工作分派(dispatch)到主队列的类型是否等同或者可能存在一些差异?
dispatch_async(dispatch_get_main_queue()) {
// Do stuff...
}
和
NSOperationQueue.mainQueue().addOperationWithBlock { [weak self] () -> Void in
// Do stuff..
}
最佳答案
有差异,但它们有些微妙。
排队到 -[NSOperationQueue mainQueue]
的操作在每次运行循环中执行一个操作。这意味着,除其他外,操作之间将有一个“绘制”过程。
使用 dispatch_async(dispatch_get_main_queue(),...)
和 -[performSelectorOnMainThread:...]
所有排队的 block /选择器在不旋转的情况下一个接一个地调用运行循环(即允许绘制 View 或类似的东西)。运行循环将在执行完所有排队的 block 后继续。
因此,关于绘图,dispatch_async(dispatch_get_main_queue(),...)
和 -[performSelectorOnMainThread:...]
将批操作合并到一个绘图 channel 中,而 -[NSOperationQueue mainQueue]
将在每次操作后绘制。
要对此进行全面、深入的调查,请参阅我的回答 over here .
关于ios - 这两种向主线程(CGD 和 NSOperationQueue)分派(dispatch)工作的方式是否等价?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35796826/