Chapters

Hide chapters

Concurrency by Tutorials

Third Edition · iOS 16 · Swift 5.7 · Xcode 14

9. Operation Dependencies
Written by Scott Grosch

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this chapter, you’re going to learn about dependencies between operations. Making one operation dependent on another provides two specific benefits for the interactions between operations:

  1. Ensures that the dependent operation does not begin before the prerequisite operation has completed.
  2. Provides a clean way to pass data from the first operation to the second operation automatically.

Enabling dependencies between operations is one of the primary reasons you’ll find yourself choosing to use an Operation over GCD.

Modular Design

Consider the tilt shift project you’ve been creating. You now have an operation that will download from the network, as well as an operation that will perform the tilt shift. You could instead create a single operation that performs both tasks, but that’s not a good architectural design.

Classes should ideally perform a single task, enabling reuse within and across projects. If you had built the networking code into the tilt shift operation directly, then it wouldn’t be usable for an already-bundled image. While you could add many initialization parameters specifying whether or not the image would be provided or downloaded from the network, that bloats the class. Not only does it increase the long-term maintenance of the class — imagine switching from URLSession to Alamofire — it also increases the number of test cases which have to be designed.

Specifying Dependencies

Adding or removing a dependency requires just a single method call on the dependent operation. Consider a fictitious example in which you’d download an image, decrypt it and then run the resultant image through a tilt shift:

let networkOp = NetworkImageOperation()
let decryptOp = DecryptOperation()
let tiltShiftOp = TiltShiftOperation()

decryptOp.addDependency(op: networkOp)
tiltShiftOp.addDependency(op: decryptOp)
tiltShiftOp.removeDependency(op: decryptOp)

Avoiding the Pyramid of Doom

Dependencies have the added side effect of making the code much simpler to read. If you tried to write three chained operations together using GCD, you’d end up with a pyramid of doom. Consider the following pseudo-code for how you might have to represent the previous example with GCD:

let network = NetworkClass()
network.onDownloaded { raw in
  guard let raw else { return }

  let decrypt = DecryptClass(raw)
  decrypt.onDecrypted { decrypted in
    guard let decrypted else { return }

    let tilt = TiltShiftClass(decrypted)
    tilt.onTiltShifted { tilted in
      guard let tilted else { return }
    }
  }
}

Watch Out for Deadlock

In Chapter 5, “Concurrency Problems,” you learned about deadlock. Any time a task is dependent on another, you introduce the possibility of deadlock, if you aren’t careful. Picture in your mind — better yet graph out — the dependency chain. If the graph draws a straight line, then there’s no possibility of deadlock.

Ruooa 0 Q W ouoyzint Ot #5 On #6 Uc #7

Yiuia 0 Qa L eagxebp Hieee 6 Uy #9 Is #3 Ac #0 Oy #4 Ih #1 Ih #9

H P uijxumfosrotj Xeoeu 8 Zueua 2 Uc #9 Af #3 Uk #1 Uj #9 Ow #1 Ih #1

Passing Data Between Operations

Now that you’ve got a way to safely make one operation depend on another, there has to be a way to pass data between them. Enter the power of protocols. The NetworkImageOperation has an output property called image. What about the case, though, in which the property is called something else?

Using Protocols

Here’s what you’re really saying: “When this operation finishes, if everything went well, I will provide you with an image of type Image.”

import UIKit

protocol ImageDataProvider {
  var image: UIImage? { get }
}

Adding Extensions

Open up NetworkImageOperation.swift and add this code to the very bottom of the file:

extension NetworkImageOperation: ImageDataProvider {}
extension TiltShiftOperation: ImageDataProvider {
  var image: UIImage? { outputImage }
}

Searching for the Protocol

The TiltShiftOperation needs a UIImage as its input. Instead of just requiring the inputImage property be set, it can now check whether any of its dependencies provides a UIImage as output.

let dependencyImage = dependencies
  .compactMap { ($0 as? ImageDataProvider)?.image }
  .first

guard
  let inputImage = inputImage ?? dependencyImage,
  let filter = TiltShiftFilter(image: inputImage, radius: 3),
  let output = filter.outputImage
else {
  return
}
init(image: UIImage? = nil) {
  inputImage = image
  super.init()
}

Updating the ImageView

Head back over to ImageView.swift and see if you can update it to download the image, tilt shift it, and then assign it to the display.

let downloadOp = NetworkImageOperation(url: url)
let op = TiltShiftOperation()
op.addDependency(downloadOp)
queue.addOperation(downloadOp)

Custom Completion Handler

The code as currently written is using the default completionBlock provided by the Operation class. You’re having to do a little extra work there to grab the image and dispatch back to the main queue. In a case like this, you may want to consider adding a custom completion block.

/// Callback which will be run *on the main thread*
/// when the operation completes.
var onImageProcessed: ((UIImage?) -> Void)?
if let onImageProcessed {
  DispatchQueue.main.async { [weak self] in
    guard let self else { return }
    onImageProcessed(outputImage)
  }
}
op.onImageProcessed = { uiImage in
  if let uiImage {
    image = Image(uiImage: uiImage)
  }
}

Where to Go From Here?

Throughout this chapter, you’ve learned how to tie the start of one operation to the completion of another. Consider where, in your existing apps, you could implement operations and operation dependencies to better modularize your app and remove the Pyramid of Doom indentation that you’ve likely implemented.

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 accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now