(iOS)dispatch_async()与NSOperationQueue [英] (iOS) dispatch_async() vs. NSOperationQueue

查看:136
本文介绍了(iOS)dispatch_async()与NSOperationQueue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于斯坦福大学的CS193p课程(在iTunes U上)以及Big Nerd Ranch的iOS编程书,我学会了iOS编程.在这两种方法中,他们都建议使用dispatch_async()dispatch_get_main_queue()等来处理线程和并发操作.但是,在WWDC 2012上有关构建并发UI的会议上,发言人建议使用NSOperationQueue.

I learned iOS programming thanks to Stanford's CS193p course (on iTunes U) as well as the iOS programming book from Big Nerd Ranch. In both of those, they recommend using dispatch_async(), dispatch_get_main_queue(), etc. to handle threading and concurrent operations. However, at WWDC 2012's session on building concurrent UI, the speaker recommended the use of NSOperationQueue.

dispatch_*()NSOperationQueue之间有什么区别,我是否应该出于某种原因(技术,性能,风格或其他原因)使用一个? NSOperationQueue只是dispatch_async周围的一个Objective-C包装器,还是比它更多的东西?

What are the differences between dispatch_*() and NSOperationQueue, and is there any reason (technical, performance, stylistic, or otherwise) that I should use one over the other? Is NSOperationQueue just an Objective-C wrapper around dispatch_async, or is there more to it than that?

推荐答案

NSOperation*类是更高级别的api.他们向您隐藏了GCD的较低级别的api,因此您可以专注于完成任务.

NSOperation* classes are the higher level api. They hide GCD's lower level api from you so that you can concentrate on getting the task done.

经验法则是:首先使用最高级别的api,然后根据需要完成的工作降级.

此方法的优点是您的代码与供应商提供的特定实现无关. 在此示例中,使用NSOperation,将使用Apple的执行排队实现(使用GCD).如果Apple决定在后台更改实现的细节,他们可以这样做而又不会破坏应用程序的代码.
一个这样的例子就是Apple弃用GCD并使用完全不同的库(这不太可能,因为Apple创建了GCD,而且每个人似乎都喜欢它).

The advantage of this approach is that your code stays most agnostic to the specific implementation the vendor provides. In this example, using NSOperation, you'll be using Apple's implementation of execution queueing (using GCD). Should Apple ever decide to change the details of implementation behind the scenes they can do so without breaking your application's code.
One such example would be Apple deprecating GCD and using a completely different library (which is unlikely because Apple created GCD and everybody seems to love it).

关于此事,我建议您咨询以下资源:

Regarding the matter i recommend consulting the following resources:

  • http://nshipster.com/nsoperation/
  • https://cocoasamurai.blogspot.de/2009/09/guide-to-blocks-grand-central-dispatch.html
  • https://cocoasamurai.blogspot.de/2009/09/making-nsoperation-look-like-gcd.html
  • https://www.raywenderlich.com/76341/use-nsoperation-nsoperationqueue-swift
  • https://developer.apple.com/documentation/foundation/operationqueue
  • Video: Advanced NSOperations, WWDC 2015, Session 226
  • Sample code: Advanced NSOperations, WWDC 2015
  • Video: Building Responsive and Efficient Apps with GCD, WWDC 2015, Session 718

现在,关于您的具体问题:

Now, regarding your specific questions:

dispatch _ *()和NSOperationQueue,[...]

What are the differences between dispatch_*() and NSOperationQueue, [...]

请参阅上文.

[...],是否有任何理由(技术上,性能上,风格上或其他方面)我应该优先使用另一个?

[...] and is there any reason (technical, performance, stylistic, or otherwise) that I should use one over the other?

如果NSOperation东西可以完成您的工作,请使用它.

If the NSOperation stuff gets your job done, use it.

NSOperationQueue只是一个围绕dispatch_async的Objective-C包装器吗?或者它还有更多呢?

Is NSOperationQueue just an Objective-C wrapper around dispatch_async, or is there more to it than that?

是的,基本上是.加上操作依赖性等功能,轻松启动/停止.

Yes, it basically is. Plus features like operation dependencies, easy start/stop.

要说的是,首先使用最高级别的api听起来很有趣.当然,如果您需要一种快速的方法来在特定线程上运行代码,则您不想编写大量的样板代码,这些代码使使用较低级别的C函数完全有效:

To say, use the highest level api first might sound intriguing. Of course, if you need a quick way to run code on a specific thread, you don't want to write a whole lot of boilerplate code which makes the use of lower level C functions perfectly valid:

dispatch_async(dispatch_get_main_queue(), ^{
  do_something();
});

但是考虑一下:

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
  do_something();
}];

我建议使用后者,因为您将要编写的大多数内容都是Objective-C,为什么不采用它的表现力呢?

I'd recommend the latter because most of what you'll write is Objective-C anyway so why not embrace its expressiveness?

这篇关于(iOS)dispatch_async()与NSOperationQueue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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