-1

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?

Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131

1 Answers1

1

Here's the documentation:

This method throws an invalidArgumentException exception if any of the whiteBalanceGains structure fields are set to unsupported values.

So it looks like you might be getting an exception that the docs specifically told you you might get.

And the same section tells you how to avoid getting that exception:

For each channel in the whiteBalanceGains structure, only values between 1.0 and maxWhiteBalanceGain are supported.

You might want to add a check for that.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I know, but how are the gains from KVO getting out of bounds is a mystery. – Deepak Sharma Oct 30 '19 at 14:10
  • Hmm. If, instead of looking in the change dictionary, you just turn to the original device and ask it what its white balance gains are now, does that make any difference? – matt Oct 30 '19 at 14:20
  • 1
    Oh, one more thought, beware of threading issues. Always a worry. – matt Oct 30 '19 at 14:21
  • Well thread safety is issue, the problem could be that videoDevice got changed elsewhere but the KVO is still coming for old videoDevice. But how do I know KVO is for which object, old or new? – Deepak Sharma Oct 30 '19 at 16:29
  • I don’t know what that even means. You get notified because the property changed. – matt Oct 30 '19 at 16:44