Chapters

Hide chapters

Concurrency by Tutorials

Third Edition · iOS 16 · Swift 5.7 · Xcode 14

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, OperationQueue 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 one 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 dispatch queue by setting isSuspended 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 TiltShift.xcodeproj from this chapter’s starter materials.

Updating the Table

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

@State private var queue = OperationQueue()
ImageView(in: queue, for: rowNumber)
let queue: OperationQueue

init(in queue: OperationQueue, for rowNumber: Int) {
  self.rowNumber = rowNumber
  self.queue = queue
}
// 1
op.completionBlock = {
  // 2
  if let outputImage = op.outputImage {
    // 3
    DispatchQueue.main.async {
      print("Updating image")
      image = Image(uiImage: outputImage)
    }
  }
}

// 4
queue.addOperation(op)
ImageView(in: OperationQueue(), for: Int.random(in: 0..<10))

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