Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 783
xonegirlz Avatar asked Nov 07 '11 06:11

xonegirlz


People also ask

Which is the best of GCD Nsthread or NSOperationQueue?

NSOperationQueue works best when tasks are discrete, synchronous and live in the same thread (eg they are more or less atomic), the queue can be used as basic thread pool though in almost any situation.

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 NSOperationQueue?

A queue that regulates the execution of operations.

What is GCD and dispatch queue?

GCD maintains tasks queues to operate them. Once a task is inserted in the queue it starts its execution. It's up to us whether we want to start this task synchronously or asynchronously. Following is the line to create a Dispatch Queue: let dispatchQueue = DispatchQueue(label: "mydispatchqueue")


2 Answers

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.

like image 176
Parag Bafna Avatar answered Oct 19 '22 23:10

Parag Bafna


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

like image 25
Catfish_Man Avatar answered Oct 20 '22 01:10

Catfish_Man