Chapters

Hide chapters

Concurrency by Tutorials

Second Edition · iOS 13 · Swift 5.1 · Xcode 11

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

7. Operation Queues
Written by Scott Grosch

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

The real power of operations begins to appear when you let an OperationQueue handle your operations. Just like with GCD’s DispatchQueue, the OperationQueue class is what you use to manage the scheduling of an Operation and the maximum number of operations that can run simultaneously.

OperationQueue allows you to add work in three separate ways:

  • Pass an Operation.
  • Pass a closure.
  • Pass an array of Operations.

If you implemented the project from the previous chapter, you saw that an operation by itself is a synchronous task. While you could dispatch it asynchronously to a GCD queue to move it off the main thread, you’ll want, instead, to add it to an OperationQueue to gain the full concurrency benefits of operations.

OperationQueue management

The operation queue executes operations that are ready, according to quality of service values and any dependencies the operation has. Once you’ve added an Operation to the queue, it will run until it has completed or been canceled. You’ll learn about dependencies and canceling operations in future chapters.

Once you’ve added an Operation to an OperationQueue, you can’t add that same Operation to any other OperationQueue. Operation instances are once and done tasks, which is why you make them into subclasses so that you can execute them multiple times, if necessary.

Waiting for completion

If you look under the hood of OperationQueue, you’ll notice a method called waitUntilAllOperationsAreFinished. It does exactly what its name suggests: Whenever you find yourself wanting to call that method, in your head, replace the word wait with block in the method name. Calling it blocks the current thread, meaning that you must never call this method on the main UI thread.

Quality of service

An OperationQueue behaves like a DispatchGroup in that you can add operations with different quality of service values and they’ll run according to the corresponding priority. If you need a refresher on the different quality of service levels, refer back to Chapter 3, “Queues & Threads.”

Pausing the queue

You can pause the operation queue by setting the isSuspended property to true. In-flight operations will continue to run but newly added operations will not be scheduled until you change isSuspended back to false.

Maximum number of operations

Sometimes you’ll want to limit the number of operations which are running at a single time. By default, the dispatch queue will run as many jobs as your device is capable of handling at once. If you wish to limit that number, simply set the maxConcurrentOperationCount property on the dispatch queue. If you set the maxConcurrentOperationCount to 1, then you’ve effectively created a serial queue.

Underlying DispatchQueue

Before you add any operations to an OperationQueue, you can specify an existing DispatchQueue as the underlyingQueue. If you do so, keep in mind that the quality of service of the dispatch queue will override any value you set for the operation queue’s quality of service.

Fix the previous project

In the previous chapter, you set up an operation to handle the tilt shift, but it ran synchronously. Now that you’re familiar with OperationQueue, you’ll modify that project to work properly. You can either continue with your existing project or open up Concurrency.xcodeproj from this chapter’s starter materials.

UIActivityIndicator

The first change you’ll make is to add a UIActivityIndicator to clue the user that something is happening. Open up the Main.storyboard and choose the Tilt Shift Table View Controller Scene. Drag an activity indicator to the center of the image so that the crosshairs appear in both directions and place it there.

@IBOutlet private weak var activityIndicator: UIActivityIndicatorView!
var isLoading: Bool {
  get { return activityIndicator.isAnimating }
  set {
    if newValue {
      activityIndicator.startAnimating()
    } else {
      activityIndicator.stopAnimating()
    }
  }
}

Updating the table

Head over to TiltShiftTableViewController.swift. In order to add operations to a queue, you need to create one. Add the following property to the top of the class:

private let queue = OperationQueue()
let op = TiltShiftOperation(image: image)
op.completionBlock = {
  DispatchQueue.main.async {
    guard let cell = tableView.cellForRow(at: indexPath) 
      as? PhotoCell else { return }

    cell.isLoading = false
    cell.display(image: op.outputImage)
  }
}

queue.addOperation(op)

Where to go from here?

The table currently loads and filters every image every time the cell is displayed. Think about how you might implement a caching solution so that the performance is even better.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.

Unlock now