为什么我应该选择GCD over NSOperation和块的高级应用程序? [英] Why should I choose GCD over NSOperation and blocks for high-level applications?

查看:131
本文介绍了为什么我应该选择GCD over NSOperation和块的高级应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple的Grand Central调度参考说明:

Apple's Grand Central Dispatch reference says:


...如果您的应用程序需要在$ b的Unix级别操作$ b系统 - 例如,如果需要处理文件描述符,Mach
端口,信号或计时器。GCD不限于系统级
应用程序,而是在将其用于更高级别应用程序,你
应该考虑类似的功能提供在Cocoa(通过
NSOperation和块对象)将更容易使用或更多
适合你的需要。。

"...if your application needs to operate at the Unix level of the system—for example, if it needs to manipulate file descriptors, Mach ports, signals, or timers. GCD is not restricted to system-level applications, but before you use it for higher-level applications, you should consider whether similar functionality provided in Cocoa (via NSOperation and block objects) would be easier to use or more appropriate for your needs.".

http://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html

I实际上不能想到情况,对于高级应用程序,其中使用GCD是强制性的,NSOperation可以/不应该使用。

I can't actually think of situations, for high-level applications, in which the use of GCD is mandatory and NSOperation could/should not be used.

任何想法? / p>

Any thoughts?

推荐答案

这里提到的问题与Chris Hanson在他的文章何时使用NSOperation与GCD

The point being made here is the same one that Chris Hanson states in his article "When to use NSOperation vs. GCD":


简单的答案是所有应用程序
开发的一般准则:

The straightforward answer is a general guideline for all application development:

始终使用可用的最高级抽象,并将
下降到较低级别

Always use the highest-level abstraction available to you, and drop down to lower-level abstractions when measurement shows that they are needed.

在这种特殊情况下,这意味着在编写Cocoa
应用程序时,通常应该使用NSOperation而不是
直接使用GCD。不是因为效率的差异,而是
,因为NSOperation在GCD的
机制之上提供了更高级别的抽象。

In this particular case, it means that when writing Cocoa applications, you should generally be using NSOperation rather than using GCD directly. Not because of a difference in efficiency, but because NSOperation provides a higher-level abstraction atop the mechanisms of GCD.

一般来说,我同意这一点。 NSOperation和NSOperationQueue提供对依赖性以及GCD块和队列没有的一两个其他事物的支持,并且它们抽象了如何实现并发操作的较低级别细节。如果你需要这个功能,NSOperation是一个很好的方式。

In general, I agree with this. NSOperation and NSOperationQueue provide support for dependencies and one or two other things that GCD blocks and queues don't have, and they abstract away the lower-level details of how the concurrent operations are implemented. If you need that functionality, NSOperation is a very good way to go.

但是,在使用两者后,我发现自己替换了所有基于NSOperation的代码与GCD块和队列。我这样做有两个原因:当使用NSOperation频繁操作时,有显着的开销,我相信我的代码是更干净,更具描述性,当使用GCD块。

However, after working with both, I've found myself replacing all of my NSOperation-based code with GCD blocks and queues. I've done this for two reasons: there is significant overhead when using NSOperation for frequent actions, and I believe my code is cleaner and more descriptive when using GCD blocks.

第一个原因来自我的应用程序中的分析,其中我发现NSOperation对象分配和释放过程在处理小型和频繁操作时占用了大量的CPU资源,例如将OpenGL ES帧渲染到屏幕。 GCD块完全消除了这种开销,导致显着的性能改进。

The first reason comes from profiling in my applications, where I found that the NSOperation object allocation and deallocation process took a significant amount of CPU resources when dealing with small and frequent actions, like rendering an OpenGL ES frame to the screen. GCD blocks completely eliminated that overhead, leading to significant performance improvements.

第二个原因是更主观的,但我相信我的代码在使用块比NSOperations更干净。一个块允许的范围的快速捕获和它们的内联特性使得更少的代码,因为你不需要创建自定义NSOperation子类或捆绑参数传递到操作,在我看来更具描述性的代码,因为你可以把代码放在一个队列中,在被释放的地方运行。

The second reason is more subjective, but I believe that my code is cleaner when using blocks than NSOperations. The quick capture of scope allowed by a block and the inline nature of them make for less code, because you don't need to create custom NSOperation subclasses or bundle up parameters to be passed into the operation, and more descriptive code in my opinion, because you can place the code to be run in a queue at the point where it is fired off.

再次,它是一个偏好,但我发现自己使用GCD更多,甚至在更抽象的Cocoa应用程序。

Again, its a matter of preference, but I've found myself using GCD more, even in otherwise more abstracted Cocoa applications.

这篇关于为什么我应该选择GCD over NSOperation和块的高级应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆