xonegirlz
xonegirlz

Reputation: 8977

NSOperationQueue vs GCD

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.

Upvotes: 13

Views: 6480

Answers (3)

CodeMaster
CodeMaster

Reputation: 1

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

Upvotes: 0

Parag Bafna
Parag Bafna

Reputation: 22930

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.

Upvotes: 20

Catfish_Man
Catfish_Man

Reputation: 41831

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

Upvotes: 7

Related Questions