Which tasks are more suitable to NSOperation than GCD? [duplicate]
Asked Answered
S

4

29

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.

Snap answered 3/12, 2010 at 11:3 Comment(0)
W
25

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.

Wit answered 3/12, 2010 at 11:10 Comment(9)
would it be fair to say that nsoperation is well suited to operations that are not time critical such as downloading a file and that gcd is well suited to time critical operations such as performing dsp on audio? Could you give me an example of when you would use one over the other?Snap
I don't think so. 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
That said, as I wrote above, I will make an 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
This is tagged iOS so.... NSOperationQueue is NOT built on top of GCD (yet). see: developer.apple.com/library/ios/#documentation/cocoa/reference/… Note: In iOS, operation queues do not use Grand Central Dispatch to execute operations. They create separate threads for non-concurrent operations and launch concurrent operations from the current thread. For a discussion of the difference between concurrent and non-concurrent operations and how they are executed, see NSOperation Class Reference.Prevocalic
Wow, you're completely right. I came from OS X background, and never carefully read the documentation of NSOperationQueue from iOS point of view... Bummer.Wit
iOS document for NSOperationQueue mentioned it's not build on GCD.Jocasta
@Robert: that's pointed out already in April, see two comments above! Sorry for not updating the answer according to the comments ...Wit
on iOS4+ NSOperationQueue use GCD, see the Class Reference.Sheeting
@Sheeting That isn't always true. From the 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
F
13

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.

Fatally answered 6/8, 2011 at 21:42 Comment(2)
I'm still not convinced this is the case. It's over 2 years since this answer and I still see this in the 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
@Answerbot As of iOS 4 and OS X 10.6, that is no longer the case. Read the last two parts of the "Overview" section developer.apple.com/library/ios/#documentation/Cocoa/Reference/…Circumambulate
S
8

As always with such questions, use the simplest API available. Measure if it's a performance problem and then reevaluate if needed.

Slop answered 3/12, 2010 at 16:52 Comment(4)
This answer would be good if you told which of the API is the simplest.Dearth
Which is the simplest rather depends on the task at handSlop
Basically, I'd like to know the simplest API of both if you don't consider the task at hand. But to provide a standard use case - imagine you have one task for the background that you don't want to have on the main thread. It should change the view when finished.Dearth
That sounds like its own question. Suggest you start by reading the docs.Slop
A
3

One thing that I don't believe has been mentioned here is that NSOperations 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.

Alvardo answered 23/11, 2011 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.