12

In what cases would you prefer to use NSOperationQueue over GCD?

From my limited experience of these two, I take it that with NSOperationQueue you basically have control over how many concurrent operations there are.

With GCD you can't do this, since you are using a queue. Except you can somehow simulate this with a multi core processor, although still I think there's no way to control it.

User97693321
  • 3,336
  • 7
  • 45
  • 69
xonegirlz
  • 8,889
  • 19
  • 69
  • 127
  • 4
    Possible duplicate of http://stackoverflow.com/questions/7146052/gcd-nsoperationqueue-or-create-a-thread-manually http://stackoverflow.com/questions/4344884/nsoperation-vs-grand-central-dispatch – 0x8badf00d Nov 07 '11 at 15:41
  • possible duplicate of [NSOperation vs Grand Central Dispatch](http://stackoverflow.com/questions/10373331/nsoperation-vs-grand-central-dispatch) – Monolo Jun 19 '13 at 18:05

3 Answers3

20

NSOperationQueue is built on GCD as of iOS 4. Use the simplest API for the task at hand.Measure if it's a performance problem and then reevaluate if needed.dispatch_async is lower level, usually C-type stuff (but not limited to), and is good for one-shot and sequential type deals. 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.

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • is NSOperationQueue a a higher API than GCD? – xonegirlz Nov 07 '11 at 15:22
  • 1
    NSOperation is built on top of 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. – Parag Bafna Nov 08 '11 at 05:35
7

I assume by NSPriorityQueue you mean NSOperationQueue? The main reasons to use NSOperationQueue over GCD are if you need its additional features:

  • Older OS support
  • KVO on operation properties
  • Dependencies
  • Queue width limiting (although you can do this fairly easily in GCD with dispatch_semaphore_t)

Otherwise, unless you're working with an API that takes an NSOperationQueue, GCD is probably a better bet

Catfish_Man
  • 41,261
  • 11
  • 67
  • 84
0

Being at higher level,I found NSOperationQueue more elegant to manage tasks/operations instead of handling them at lower level using GCD.