ios - 这两种向主线程(CGD 和 NSOperationQueue)分派(dispatch)工作的方式是否等价?

标签 ios multithreading grand-central-dispatch nsoperation

我很好奇这两种将工作分派(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/

相关文章:

java - RxJava : How to get all results AND errors from an Observable

grand-central-dispatch - 对自己使用 dispatch_async

ios - 在ios应用程序中根据不同输入切换UIViewControllers的最佳方法

java - 当线程 T1 加入线程 T2 并且 T2 被中断时会发生什么

java - 用Jsoup填充数组的方法不等待jsoup完成网站请求

ios - 在多个异步方法之后执行代码

ios - 使用 GCD 在 block 内赋值

ios - Firebase 和 Google Signin 给出重复符号错误

iphone - 如何让处理程序重复 UIView animateWithDuration?

android - Wifi 直连发现