In what cases would you prefer to use NSOperationQueue
or NSOperation
or GCD.
I have just started working in thread so what are the benefits and time complexity.
When we need to use which one and what are the benefits and in which cases we need to consider which one .
Asked
Active
Viewed 1,684 times
0

Rob
- 415,655
- 72
- 787
- 1,044
-
I'd refer you to WWDC 2015 video [Advanced NSOperations](https://developer.apple.com/videos/wwdc/2015/?id=226). Also see [Concurrency Programming Guide](https://developer.apple.com/library/mac/documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091-CH1-SW1) which walks through both. Also, if you search Stack Overflow for "NSOperation GCD", you'll get a ton of hits. – Rob Aug 21 '15 at 13:13
1 Answers
1
GCD is low level C-based API.
NSOperation
and NSOperationQueue
are Objective-C classes that are based on GCD and simplify execution prioritisation and cancellation.
Advantages of NSOperationQueue
over GCD include:
It is easy to cancel or suspend an operation if it is in queue it can be stopped if it is running.
You can define the maximum number of concurrent operations.
You can make dependencies between distinct instances of
NSOperation
.
-
Also it worth to mention that NSOperation internals based on GCD, so you won't get any performance issues if choose it. – Roman Kabachenko Aug 21 '15 at 12:31
-
So do u have any sample examples to demonstrate when we need to use which and which one is best in which scenario. – Aug 21 '15 at 12:50
-
Gamex- Re cancel, GCD now supports cancellation, too. Re suspend, you can suspend both custom dispatch queues and operation queues. Re `NSOperation` features, perhaps I'd add dependencies, the ability to wrap asynchronous task in an asynchronous `NSOperation` subclass, etc. – Rob Aug 21 '15 at 13:10
-
@Darshan Dispatch queues are indespensible for low-level tasks (synchronizing, background thread timers, dispatch sources for decoupling fast background tasks from UI, etc.). Operation queues are wonderful for high-level operations where you have dependencies and/or might need to constrain the degree of concurrency. For the vast majority of asynchronous tasks that fall in the middle-ground, you often safely use either one and it's a matter of personal preference. – Rob Aug 21 '15 at 13:27
-
Still i m lil bit confusion about in both GCD and NSOpration Queue, so i would request you to could you please explain me in any practical scenario @ Roman Kabachenko. – Darshan Aug 24 '15 at 08:14