NSThread与NSOperationQueue与???在iPhone上 [英] NSThread vs. NSOperationQueue vs. ??? on the iPhone

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

问题描述

当前,我正在使用NSThread在另一个线程中缓存图像.

Currently I'm using NSThread to cache images in another thread.

[NSThread detachNewThreadSelector:@selector(cacheImage:) toTarget:self withObject:image];

或者:

[self performSelectorInBackground:@selector(cacheImage:) withObject:image];

或者,我可以使用NSOperationQueue

NSInvocationOperation *invOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(cacheImage:) object:image];
NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
[opQueue addOperation:invOperation];

是否有任何理由要退出NSThread? GCD在iPhone上发布时是第四个选择,但是除非性能有显着提高,否则我还是会坚持在大多数平台上都可以使用的方法.

Is there any reason to switch away from NSThread? GCD is a 4th option when it's released for the iPhone, but unless there's a significant performance gain, I'd rather stick with methods that work in most platforms.

根据@ Jon-Eric的建议,我使用了NSOperationQueue/NSOperation子类解决方案.效果很好. NSOperation类非常灵活,您可以根据需要将其与调用,块或自定义子类一起使用.无论您如何创建NSOperation,都可以在准备运行它时将其放入操作队列中.这些操作旨在用作您放入队列中的对象,也可以根据需要作为独立的异步方法运行它们.由于您可以轻松轻松地同步运行自定义操作方法,因此测试非常容易.

Based on @Jon-Eric's advice, I went with an NSOperationQueue/NSOperation subclass solution. It works very well. The NSOperation class is flexible enough that you can use it with invocations, blocks or custom subclasses, depending on your needs. No matter how you create your NSOperation you can just throw it into an operation queue when you are ready to run it. The operations are designed to work as either objects you put into a queue or you can run them as standalone asynchronous methods, if you want. Since you can easily run your custom operation methods synchronously, testing is trivially easy.

自从我问了这个问题以来,我已经在少数几个项目中使用了相同的技术,而我对它保持代码和测试的整洁,有条理和愉快地异步的方式感到满意.

I've used this same technique in a handful of projects since I asked this question and I couldn't be happier with the way it keeps my code and my tests clean, organized and happily asynchronous.

A ++++++++++ 会再次继承

A++++++++++ Would subclass again

推荐答案

通常,使用NSOperationQueue可以获得更好的里程.

In general you'll get better mileage with NSOperationQueue.

三个具体原因:

  • 您可能希望一次启动许多项目的缓存. NSOperationQueue足够聪明,它只能创建与内核一样多的线程,从而对其余操作进行排队.使用NSThread,创建100个线程来缓存100个图像可能是过大的,并且效率低下.
  • 您可能要取消cacheImage操作.使用NSOperationQueue可以更轻松地实现取消;大部分工作已经为您完成.
  • NSOperationQueue现在或将来可以自由切换到更智能的实现(例如Grand Central Dispatch). NSThread更可能始终只是操作系统线程.
  • You may want to initiate caching of many items at once. NSOperationQueue is smart enough to only create about as many threads as there are cores, queuing the remaining operations. With NSThread, creating 100 threads to cache 100 images is probably overkill and somewhat inefficient.
  • You may want to cancel the cacheImage operation. Implementing cancellation is easier with NSOperationQueue; most the work is already done for you.
  • NSOperationQueue is free to switch to a smarter implementation (like Grand Central Dispatch) now or in the future. NSThread is more likely to always be just an operating system thread.

奖金:

  • NSOperationQueue内置了其他一些不错的结构,例如尊重操作优先级和依赖项的复杂方法.
  • NSOperationQueue has some other nice constructs built-in, such as a sophisticated way of honoring operation priorities and dependencies.

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

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