NSThread vs. NSOperationQueue vs. ??? on the iPhone -
Currently I am using NSThread
to cache images in another thread .
[NSThread detachNewThreadSelector: @selector (cacheImage :) toTarget: Object with self: image];
Alternatively:
[self display selector Inbackground: @Selector (cash image :) with object: image);
Alternatively, I have NSOperationQueue
NSInvocationOperation * invOperation = [[NSInvocationOperation alloc] initWithTarget: Self Selector: @ Can select selector (cache image) object: image]; NSOperationQueue * opQueue = [[NSOperationQueue alloc] init]; [Octuion operation: inoperation]; Is there a reason to switch away from NSThread
? GCD is a fourth option when it is released to the iPhone, but unless there is a significant performance gain, I stick to the ways that work in most platforms. Based on the advice of @John-Eric, I went with a NSOperationQueue
/ NSOperation
subclass solution. it works very well. The NSOption
class is flexible enough so that you can use it with inoculation, block or custom subclasses depending on your needs. No matter how you make your NSOperation
, you can only put it in the operation queue when you're ready to run it. Operations are designed to work either as objects that you put in a queue or you can run them as a standalone asynchronous method, if you want, because you can easily synchronize your custom operation methods By the way, test testing is trivial. I have used this technique by asking some questions, and I was not happy in this way that it keeps my code and my tests clean, organized and happily asynchronous.
A ++++++++++ will again be a sub-class
Normally you will get better return with the NSOperationQueue
Three specific reasons:
- You want to start caching at the same time many items in the
NSOperationQueue
enough to create too many sources Because the core is enough, with theNSThread
queuing the rest of the work, 100 images are possibly overkill and somewhat incompatible to cache 100 images. - You may want to cancel the
cacheImage
operation. Cancellation is easy to implementNSOperationQueue
; Most of the work has already been done for you. -
NSOction Q
is now free to switch to smart implementation (such as the Grand Central Dispatch) now or in the future.NSThread
is always likely to be an operating system thread.
Bonus:
-
NSOperationQueue
contains some other good works such as operating awards and honoring dependency Sophisticated way
Comments
Post a Comment