Which tasks would be better suited to using NSOperation
as opposed to using GCD
when programming for the iPhone?
To me they seem to do the same thing. I can't see the strengths and weaknesses one has over the other.
Which tasks would be better suited to using NSOperation
as opposed to using GCD
when programming for the iPhone?
To me they seem to do the same thing. I can't see the strengths and weaknesses one has over the other.
NSOperation
is built on top of GCD, so the question is more about whether you use NSOperation
or pass a block directly to GCD.
An NSOperation
is bulky and needs more boiler-plate codes to set it up, but it has a lot more functionality. You can create the same NSOperation
subclass in various parts of your code and put them into the queue and run it.
Passing a block to GCD by e.g. dispatch_async
is quick and disposable. You typically don't reuse a block anywhere else; you just set up a block which is executed only at that point of the code, passes it to the GCD or other APIs, and quickly go on.
So each has its merits.
NSOperation
definitely has slightly more overhead, but that doesn't make GCD suited to time critical operations. Again, NSOperation
(in the main queue) is a thin layer over GCD. So your question doesn't make sense. Both are GCD. –
Wit NSOperation
if I create and run the same background task in various parts of the program. I will just use dispatch_async
if the task is only run from a single part of the program. –
Wit NSOperationQueue
from iOS point of view... Bummer. –
Wit NSOperationQueue
use GCD, see the Class Reference. –
Sheeting An operation queue executes its operations either directly, by running them on secondary threads, **or** indirectly using the libdispatch library (also known as Grand Central Dispatch)
–
Assess Apparently, NSOperationQueue is built on GCD as of iOS 4; the docs just haven't been updated. Check this posting by an Apple employee here: https://devforums.apple.com/message/352770 (You may need to create an account) So, you should follow Mike Abdullah's advice and use the simplest API for the task at hand. dispatch_async is lower level, usually C-type stuff (but not limited to), and is good for one-shot and sequential type deals (fire this block on this queue, FTW). NSOperationQueues are higher level, Objective-C stuff, and are good if you are adding a lot of operations at various points in your code, and/or need to manage concurrency, priorities and dependencies. At least that's how I use them.
NSOperations
docs: An operation queue executes its operations either directly, by running them on secondary threads, **or** indirectly using the libdispatch library (also known as Grand Central Dispatch).
–
Assess As always with such questions, use the simplest API available. Measure if it's a performance problem and then reevaluate if needed.
One thing that I don't believe has been mentioned here is that NSOperation
s can be cancelled during execution, whereas a block is guaranteed to complete once execution has begun. Having said that, a GCD queue can be suspended (dispatch_suspend()
), so that any blocks following the currently executing blocks will not be executed.
© 2022 - 2024 — McMap. All rights reserved.