我正在学习iOS的并发编程。到目前为止,我已经了解了NSOperation/NSOperationQueue和GCD。为什么在GCD上使用NSOperationQueue,反之亦然?

听起来好像GCD和NSOperationQueue都从用户那里抽象出了nsthread的显式创建。然而,这两种方法之间的关系对我来说并不清楚,所以任何反馈都很感激!


当前回答

GCD是一个低级的基于c语言的API,可以非常简单地使用基于任务的并发模型。NSOperation和NSOperationQueue是Objective-C类,做类似的事情。NSOperation是最先被引入的,但是从10.5和iOS 2开始,NSOperationQueue和它的朋友是通过GCD内部实现的。

一般来说,您应该使用适合您需要的最高级别的抽象。这意味着你通常应该使用NSOperationQueue而不是GCD,除非你需要做一些NSOperationQueue不支持的事情。

Note that NSOperationQueue isn't a "dumbed-down" version of GCD; in fact, there are many things that you can do very simply with NSOperationQueue that take a lot of work with pure GCD. (Examples: bandwidth-constrained queues that only run N operations at a time; establishing dependencies between operations. Both very simple with NSOperation, very difficult with GCD.) Apple's done the hard work of leveraging GCD to create a very nice object-friendly API with NSOperation. Take advantage of their work unless you have a reason not to.

警告: 另一方面,如果你真的只需要发送一个块,而不需要NSOperationQueue提供的任何附加功能,那么使用GCD也没有什么问题。只要确保它是适合这项工作的工具。

其他回答

根据我对一个相关问题的回答,我不同意BJ的观点,建议你先看看GCD而不是NSOperation / NSOperationQueue,除非后者提供了一些GCD不提供的你需要的东西。

在GCD之前,我在我的应用程序中使用了很多NSOperations / NSOperationQueues来管理并发。然而,自从我开始在常规的基础上使用GCD,我几乎完全用块和分派队列替换了NSOperations和NSOperationQueues。这来自于我在实践中使用这两种技术的方式,以及我对它们进行的分析。

First, there is a nontrivial amount of overhead when using NSOperations and NSOperationQueues. These are Cocoa objects, and they need to be allocated and deallocated. In an iOS application that I wrote which renders a 3-D scene at 60 FPS, I was using NSOperations to encapsulate each rendered frame. When I profiled this, the creation and teardown of these NSOperations was accounting for a significant portion of the CPU cycles in the running application, and was slowing things down. I replaced these with simple blocks and a GCD serial queue, and that overhead disappeared, leading to noticeably better rendering performance. This wasn't the only place where I noticed overhead from using NSOperations, and I've seen this on both Mac and iOS.

Second, there's an elegance to block-based dispatch code that is hard to match when using NSOperations. It's so incredibly convenient to wrap a few lines of code in a block and dispatch it to be performed on a serial or concurrent queue, where creating a custom NSOperation or NSInvocationOperation to do this requires a lot more supporting code. I know that you can use an NSBlockOperation, but you might as well be dispatching something to GCD then. Wrapping this code in blocks inline with related processing in your application leads in my opinion to better code organization than having separate methods or custom NSOperations which encapsulate these tasks.

NSOperations和NSOperationQueues仍然有很好的用途。GCD没有真正的依赖关系概念,NSOperationQueues可以建立相当复杂的依赖关系图。我在少数情况下使用NSOperationQueues。

总的来说,虽然我通常主张使用最高级别的抽象来完成任务,但在这种情况下,我主张使用GCD的低级API。在与我交谈过的iOS和Mac开发者中,绝大多数人选择使用GCD而不是NSOperations,除非他们的目标操作系统版本不支持GCD (iOS 4.0和Snow Leopard之前的版本)。

GCD是一个低级的基于c语言的API,可以非常简单地使用基于任务的并发模型。NSOperation和NSOperationQueue是Objective-C类,做类似的事情。NSOperation是最先被引入的,但是从10.5和iOS 2开始,NSOperationQueue和它的朋友是通过GCD内部实现的。

一般来说,您应该使用适合您需要的最高级别的抽象。这意味着你通常应该使用NSOperationQueue而不是GCD,除非你需要做一些NSOperationQueue不支持的事情。

Note that NSOperationQueue isn't a "dumbed-down" version of GCD; in fact, there are many things that you can do very simply with NSOperationQueue that take a lot of work with pure GCD. (Examples: bandwidth-constrained queues that only run N operations at a time; establishing dependencies between operations. Both very simple with NSOperation, very difficult with GCD.) Apple's done the hard work of leveraging GCD to create a very nice object-friendly API with NSOperation. Take advantage of their work unless you have a reason not to.

警告: 另一方面,如果你真的只需要发送一个块,而不需要NSOperationQueue提供的任何附加功能,那么使用GCD也没有什么问题。只要确保它是适合这项工作的工具。

GCD非常容易使用——如果你想在后台做一些事情,你所需要做的就是编写代码并在后台队列上分派它。用NSOperation做同样的事情需要大量额外的工作。

The advantage of NSOperation is that (a) you have a real object that you can send messages to, and (b) that you can cancel an NSOperation. That's not trivial. You need to subclass NSOperation, you have to write your code correctly so that cancellation and correctly finishing a task both work correctly. So for simple things you use GCD, and for more complicated things you create a subclass of NSOperation. (There are subclasses NSInvocationOperation and NSBlockOperation, but everything they do is easier done with GCD, so there is no good reason to use them).

NSQueueOperations和GCD都允许在后台通过释放UI Application Main Tread在不同的线程上执行繁重的计算任务。

好吧,根据之前的文章,我们看到NSOperations有addDependency,这样你就可以把你的操作依次排队。

但是我也读过关于GCD的串行队列,你可以使用dispatch_queue_create在队列中创建运行你的操作。这将允许以顺序的方式一个接一个地运行一组操作。

NSQueueOperation相对于GCD的优势:

它允许添加依赖项,也允许删除依赖项,因此对于一个事务,您可以使用依赖项连续运行,而对于其他事务,则可以在GCD中并发运行 不允许这样跑。 如果某个操作在队列中,则很容易取消该操作;如果某个操作正在运行,则可以停止该操作。 可定义最大并发操作数。 您可以暂停正在队列中的操作 您可以发现队列中有多少挂起的操作。

选择NSOperation而不是GCD的另一个原因是NSOperation的取消机制。例如,一个像500px这样显示数十张照片的App,使用NSOperation我们可以在滚动表格视图或集合视图时取消对不可见图像单元格的请求,这可以极大地提高App性能并减少内存占用。GCD不容易支持这一点。

同样使用NSOperation,可以实现KVO。

这里有一篇来自Eschaton的文章值得一读。