gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:31:38 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com