NSThread vs. NSOperationQueue vs. ??? on the iPhone
Asked Answered
G

2

30

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

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

Alternately:

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

Alternately, I can use an NSOperationQueue

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

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.


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++++++++++ Would subclass again

Geerts answered 15/6, 2010 at 0:24 Comment(2)
You could profile each of them. That would be one way to find out.Fatherhood
Performance was the wrong word, I suspect they're all the same underneath. I'm more looking for someone who has experience with two or more of these methods to give me some advice on the preferred way and why it's better.Geerts
S
33

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.
Sanction answered 15/6, 2010 at 2:36 Comment(2)
Assuming you don't need the extras the NSOperationQueue provides, Why is creating 100 NSThreads inefficient? Or are you assuming if 100 threads need to be created, there must be a need for some NSOperationQueue goodies?Semester
@Tony: Creating a thread is very expensive. developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/…Geerts
U
5

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.

Unintelligible answered 15/6, 2010 at 2:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.