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

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


当前回答

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

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

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

其他回答

Well, NSOperations are simply an API built on top of Grand Central Dispatch. So when you’re using NSOperations, you’re really still using Grand Central Dispatch. It’s just that NSOperations give you some fancy features that you might like. You can make some operations dependent on other operations, reorder queues after you sumbit items, and other things like that. In fact, ImageGrabber is already using NSOperations and operation queues! ASIHTTPRequest uses them under the hood, and you can configure the operation queue it uses for different behavior if you’d like. So which should you use? Whichever makes sense for your app. For this app it’s pretty simple so we just used Grand Central Dispatch directly, no need for the fancy features of NSOperation. But if you need them for your app, feel free to use it!

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).

GCD是一个低级的基于c语言的API。 NSOperation和NSOperationQueue是Objective-C类。 NSOperationQueue是GCD上的objective C包装器。 如果你在使用NSOperation,那么你就隐式地使用了中央调度。

GCD相对于NSOperation的优势: 我实现。 对于GCD的实现是非常轻量级的 NSOperationQueue是复杂且重量级的

NSOperation相对于GCD的优势:

i.控制运行 你可以暂停,取消,恢复一个NSOperation

2依赖关系 你可以在两个NSOperations之间建立依赖关系 操作将不会启动,直到它的所有依赖项为finished返回true。

3运行状态 可以监视操作或操作队列的状态。 准备、执行或完成

iv.最大操作次数 您可以指定可以同时运行的排队操作的最大数量

什么时候去GCD或NSOperation 当你想要更多的控制队列(所有上面提到的)使用NSOperation 对于简单的情况,你想要更少的开销 (你只是想做一些“进入后台”的工作,很少额外的工作)使用GCD

裁判: https://cocoacasts.com/choosing-between-nsoperation-and-grand-central-dispatch/ http://iosinfopot.blogspot.in/2015/08/nsthread-vs-gcd-vs-nsoperationqueue.html http://nshipster.com/nsoperation/

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

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

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

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

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

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

NSQueueOperation相对于GCD的优势:

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