(iOS) dispatch_async () vs NSOperationQueue

I learned iOS programming with the Stanford CS193p course (on iTunes U), as well as the Big Nerd Ranch iOS programming book. In both cases, it is recommended to use dispatch_async() , dispatch_get_main_queue() , etc. For processing threads and concurrent operations. However, at a WWDC 2012 session on creating a parallel user interface, the speaker recommended using NSOperationQueue .

What is the difference between dispatch_*() and NSOperationQueue , and is there any reason (technical, performance, style, or otherwise) that I should use one above the other? Is NSOperationQueue Objective-C wrapper around dispatch_async , or is there more than that?

+44
grand-central-dispatch nsoperationqueue
Jul 26 '12 at 19:27
source share
4 answers

NSOperation* classes are a higher level of api. They hide from you a lower-level GCD api so you can focus on the task.

Rule of thumb: First use the highest level api and then degrade based on what you need to accomplish.

The advantage of this approach is that your code remains the most agnostic for the particular implementation provided by the provider. In this example, using NSOperation , you will use Apple's implementation of the execution queues (using GCD). If Apple ever decides to change implementation details behind the scenes, they can do it without breaking application code.
One such example would be Apple, which depreciates the GCD and uses a completely different library (which is unlikely because Apple created the GCD, and everyone seems to love it).

Regarding the question, I recommend referring to the following resources:

Now, regarding your specific questions:

What is the difference between sending _ * () and NSOperationQueue, [...]

See above.

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

If NSOperation does your job, use it.

Is NSOperationQueue just an Objective-C wrapper around dispatch_async, or is there something else for this?

Yes, it is basically. Plus features like operational dependencies, simple start / stop.

Change

It is said that using the highest level of api may seem intriguing. Of course, if you need a quick way to run code in a specific thread, you don’t want to write many code templates, which makes the use of lower-level C functions perfectly acceptable:

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

But consider this:

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

I would recommend the latter, because most of what you write is Objective-C, so why not accept its expressiveness?

+71
Nov 06
source share

NSOperationQueue is much heavier than dispatch_async (), it is only based on GCD in a very limited way (in fact, it just uses the global dispatch queue to perform its asynchronous operations, but otherwise it does not use any other GCD tools).

NSOperationQueue has additional features not provided by GCD, but if you do not need it, using GCD will give you better performance.

+11
Jul 27 2018-12-12T00:
source share

They both do the same thing, but the main difference between them is: We can cancel the task if we want using NSOperation, whereas if we use GCD, then once we assign the task to the queue, we cannot cancel it .

+9
Aug 29 '13 at 7:18
source share

Adding to the answer above, another advantage of the NSOperation queue over the GCD:

1) Dependencies:. We can configure the dependency between the two NSOperations operation will not start until all its dependencies return to completion.

2) Operation status:. We can monitor the status of an operation or operation queue. Done, in progress, or completed

3) The maximum number of operations: - We can specify the maximum number of operations in the queue that can be performed simultaneously

+1
Mar 30 '17 at 11:51
source share



All Articles