Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(iOS) dispatch_async() vs. NSOperationQueue

I learned iOS programming thanks to Stanford's CS193p course (on iTunes U) as well as the iOS programming book from Big Nerd Ranch. In both of those, they recommend using dispatch_async(), dispatch_get_main_queue(), etc. to handle threading and concurrent operations. However, at WWDC 2012's session on building concurrent UI, the speaker recommended the use of NSOperationQueue.

What are the differences between dispatch_*() and NSOperationQueue, and is there any reason (technical, performance, stylistic, or otherwise) that I should use one over the other? Is NSOperationQueue just an Objective-C wrapper around dispatch_async, or is there more to it than that?

like image 661
Donald Burr Avatar asked Jul 26 '12 19:07

Donald Burr


People also ask

What is the difference between GCD and NSOperationQueue in iOS?

GCD is a low-level C-based API that enables very simple use of a task-based concurrency model. NSOperation and NSOperationQueue are Objective-C classes that do a similar thing. NSOperation was introduced first, but as of 10.5 and iOS 2, NSOperationQueue and friends are internally implemented using GCD .

Which is the best of GCD Nsthread or NSOperationQueue?

There is no generic "best" out of the three.

What is difference between dispatch queue and NSOperationQueue?

NSOperationQueue can be more suitable for long-running operations that may need to be cancelled or have complex dependencies. GCD dispatch queues are better for short tasks that should have minimum performance and memory overhead.

What is NSOperation and NSOperationQueue in iOS?

Overview. An operation queue invokes its queued NSOperation objects based on their priority and readiness. After you add an operation to a queue, it remains in the queue until the operation finishes its task. You can't directly remove an operation from a queue after you add it. Note.


2 Answers

NSOperation* classes are the higher level api. They hide GCD's lower level api from you so that you can concentrate on getting the task done.

The rule of thumb is: Use the api of the highest level first and then degrade based on what you need to accomplish.

The advantage of this approach is that your code stays most agnostic to the specific implementation the vendor provides. In this example, using NSOperation, you'll be using Apple's implementation of execution queueing (using GCD). Should Apple ever decide to change the details of implementation behind the scenes they can do so without breaking your application's code.
One such example would be Apple deprecating GCD and using a completely different library (which is unlikely because Apple created GCD and everybody seems to love it).

Regarding the matter i recommend consulting the following resources:

  • http://nshipster.com/nsoperation/
  • https://cocoasamurai.blogspot.de/2009/09/guide-to-blocks-grand-central-dispatch.html
  • https://cocoasamurai.blogspot.de/2009/09/making-nsoperation-look-like-gcd.html
  • https://www.raywenderlich.com/76341/use-nsoperation-nsoperationqueue-swift
  • https://developer.apple.com/documentation/foundation/operationqueue
  • Video: Advanced NSOperations, WWDC 2015, Session 226
  • Sample code: Advanced NSOperations, WWDC 2015
  • Video: Building Responsive and Efficient Apps with GCD, WWDC 2015, Session 718

Now, regarding your specific questions:

What are the differences between dispatch_*() and NSOperationQueue, [...]

See above.

[...] and is there any reason (technical, performance, stylistic, or otherwise) that I should use one over the other?

If the NSOperation stuff gets your job done, use it.

Is NSOperationQueue just an Objective-C wrapper around dispatch_async, or is there more to it than that?

Yes, it basically is. Plus features like operation dependencies, easy start/stop.

Amendment

To say, use the highest level api first might sound intriguing. Of course, if you need a quick way to run code on a specific thread, you don't want to write a whole lot of boilerplate code which makes the use of lower level C functions perfectly valid:

dispatch_async(dispatch_get_main_queue(), ^{   do_something(); }); 

But consider this:

[[NSOperationQueue mainQueue] addOperationWithBlock:^{   do_something(); }]; 

I'd recommend the latter because most of what you'll write is Objective-C anyway so why not embrace its expressiveness?

like image 153
glasz Avatar answered Sep 28 '22 03:09

glasz


NSOperationQueue is much heavier weight than dispatch_async(), it is only based on GCD in a very limited way (essentially it just uses the global dispatch queue to execute its asynchronous operations but otherwise uses no other GCD facilities).

NSOperationQueue does have additional capabilities not provided by GCD, but if you don't need these, using GCD will give you better performance.

like image 21
das Avatar answered Sep 28 '22 04:09

das