I am getting a warning in this code (the code is distilled from the actual version to remove noise):
import AVFoundation
actor MyModel {
private let device: AVCaptureDevice
init(device: AVCaptureDevice) { self.device = device }
private var observation: NSKeyValueObservation?
func doSomething<T>(_ keyPath: KeyPath<AVCaptureDevice, T>) {
let newValue = device[keyPath: keyPath]
print(newValue)
// do something here
}
func makeObservation<T: Sendable & Equatable>(keyPath: KeyPath<AVCaptureDevice, T>) {
// 🔶 workaround 1
// let keyPath = unsafeBitCast(keyPath, to: (Sendable & KeyPath<AVCaptureDevice, T>).self)
self.observation = device.observe(keyPath) { _, _ in
Task {
await self.doSomething(keyPath)
// ⚠️ Capture of 'keyPath' with non-Sendable type 'KeyPath<AVCaptureDevice, T>' in a '@Sendable' closure
}
}
}
}
// 🔶 workaround 2
// extension KeyPath: @unchecked @retroactive Sendable {}
The warning is:
⚠️ Capture of 'keyPath' with non-Sendable type 'KeyPath<AVCaptureDevice, T>' in a '@Sendable' closure
Fair enough, KeyPath is not sendable. I have these three options in mind to deal with it:
- live with the warning (until – if ever – it becomes an error)
- use workaround 1 (unsafeBitCast key path to be sendable)
- use workaround 2 (retroactively conform KeyPath to unchecked sendable)
Neither of these look ideal, and 2 & 3 seem quite dangerous.
What is the best way coping with this warning?