OperationQueue

  • OperationQueue is a prioritised FIFO queue
  • It lets you control precisely how many simultaneous operations can run
  • What quality of service you need
  • also letting you schedule work using closures
// Specify max number of operations
queue.maxConcurrentOperationCount = 4

// This won’t cancel any operations that are currently in flight.
queue.cancelAllOperations()

How operation works

  • Add your operation to the OperationQueue
  • The main method of the operation will be called by the OperationQueue automatically

In order to benefit from the cancelation feature exposed by operation queues, you should regularly check the isCancelled property for longer-running operations:

OperationQueue vs GCD

  • Operation and OperationQueue are built on top of GCD.
  • Canceling or suspending an operation is easy
  • Adding dependency among blocks can be a headache.

  • An operation can have dependencies to other operations and that is a powerful feature Grand Central Dispatch lacks.

  • If you need to perform several tasks in a specific order, then operations are a good solution.

results matching ""

    No results matching ""