Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operation Queue vs Dispatch Queue for iOS Application

  1. What are the differences between Operation Queue and Dispatch Queue?
  2. Under what circumstances will it be more appropriate to use each?
like image 465
Lopper Avatar asked Aug 16 '11 13:08

Lopper


People also ask

What is difference between operation queue and dispatch queue?

Here are how operation queues are different from dispatch queues: In operation queues, you can set priority for your operations and also you can add dependencies to the operations which means you can define that some operation execute only after the completion of other operations.

What is Operation queue in iOS?

An operation queue invokes its queued Operation objects based on their priority and readiness. After you add an operation to a queue, it remains in the queue until the operation finishes its task. You can't directly remove an operation from a queue after you add it. Note.

What is dispatch queue in iOS?

Dispatch queues are FIFO queues to which your application can submit tasks in the form of block objects. Dispatch queues execute tasks either serially or concurrently. Work submitted to dispatch queues executes on a pool of threads managed by the system.

How many types of queues are there in iOS?

There are four such queues with different priorities : high, default, low, and background.


2 Answers

OperationQueue internally uses Grand Central Dispatch and on iOS.

OperationQueue gives you a lot more control over how your operations are executed. You can define dependencies between individual operations for example, which isn't possible with plain GCD queues. It is also possible to cancel operations that have been enqueued in an OperationQueue (as far as the operations support it). When you enqueue a block in a GCD dispatch queue, it will definitely be executed at some point.

To sum it up, OperationQueue 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.

like image 165
omz Avatar answered Sep 28 '22 19:09

omz


  • Prefer GCD where task is not much complex and optimum CPU performance is required.
  • Prefer NSOperationQueue where task is complex and requires canceling or suspending a block and dependency management.

GCD is a lightweight way to represent units of work that are going to be executed concurrently. You don’t schedule these units of work; the system takes care of scheduling for you. Adding dependency among blocks can be a headache. Canceling or suspending a block creates extra work for you as a developer!

NSOperation and NSOperationQueue add a little extra overhead compared to GCD, but you can add dependency among various operations. You can re-use operations, cancel or suspend them. NSOperation is compatible with Key-Value Observation (KVO); for example, you can have an NSOperation start running by listening to NSNotificationCenter.

NSOperation and NSOperationQueue are higher lever APIs, made on top of the GDC itself, to achieve the concurrency in an object oriented way.

For detailed explanation, refer this question: https://stackoverflow.com/questions/10373331/nsoperation-vs-grand-central-dispatch

like image 23
Nitesh Borad Avatar answered Sep 28 '22 19:09

Nitesh Borad