What's the best way to handle view controller teardowns in Swift 6?

Previously we could have some code in deinit that tears down local state:

class ViewController: UIViewController {
  var displayLink: CADisplayLink?

  deinit {
    displayLink?.invalidate()
  }
}

However this doesn't work in Swift 6 because we cannot access property 'displayLink' with a non-sendable type 'CADisplayLink?' from non-isolated context in deinit.

What's the right way to resolve this? Is the following a reasonable approach using Task to create an async context?

deinit {
  Task {
    await MainActor.run {
      displayLink?.invalidate()
    }
  }
}
What's the best way to handle view controller teardowns in Swift 6?
 
 
Q