Search code examples
iosmultithreadingcocoa-touchgrand-central-dispatchnsoperationqueue

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.


Solution

  • 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.