I have the following code to observe white balance changes via KVO in Swift.
self.addObserver(self, forKeyPath: "videoInput.device.deviceWhiteBalanceGains", options: [.new, .old], context: &whitebalanceGainsObserverContext)
And then in observeValue(...), I do this:
if context == &whitebalanceGainsObserverContext {
if let newNSValue = change?[.newKey] as? NSValue {
var gains = AVCaptureDevice.WhiteBalanceGains()
newNSValue.getValue(&gains)
/* Crashes here on some devices in AppStore, throws an exception */
let newTemperatureAndTint = self.videoInput?.device.temperatureAndTintValues(for: gains)
}
}
I am never able to reproduce the crash, so I want to know how to avoid the crash. What checks do I put to avoid the exception that gets thrown?
EDIT: I also tried to use new observation API as follows:
deviceWBGainsObservation = observe(\.videoInput?.device.deviceWhiteBalanceGains, options: [.old, .new]) { (obj, change) in
if let newNSValue = change.newValue {
}
}
And even this,
deviceWBGainsObservation = videoDevice?.observe(\.deviceWhiteBalanceGains, options: [.old, .new]) {[unowned self] (object, change) in
if let newNSValue = change.newValue {
}
}
And also this:
private var videoDevice:AVCaptureDevice? {
didSet {
deviceWBGainsObservation = videoDevice?.observe(\.deviceWhiteBalanceGains, options: [.old, .new]) {[unowned self] (object, change) in
if let newNSValue = change.newValue {
}
}
}
The problem is change value is always nil in this case. Why is it?