NSThread vs. NSOperationQueue vs. ??? on the iPhone

二次信任 提交于 2019-11-27 17:27:38

In general you'll get better mileage with NSOperationQueue.

Three specific reasons:

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

Bonus:

  • NSOperationQueue has some other nice constructs built-in, such as a sophisticated way of honoring operation priorities and dependencies.

I would use NSOperationQueue. Under OS 3.2, NSOperationQueue uses threads under the hood, so the two methods should perform similarly. However, under Mac OS 10.6, NSOperationQueue uses GCD under the hood and so has the advantage of not having the overhead of separate threads. I haven't looked at the docs for OS 4, but I'd suspect it does something similar--in any case, NSOperationQueue could swap implementations if/when the performance advantages of GCD become available for the iPhone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!