I have a box that the user is able to pan around. For that, I add an observer to check if it's center has changed:
self.boxView!.addObserver(self, forKeyPath: "center", options: .old, context: &BoxCenterContext)
This gets added after an animation that presents the box.
When the box dismisses, I remove it as this:
self.boxView!.removeObserver(self, forKeyPath: "center", context: &BoxCenterContext)
There is a possibility of the user being able to dismiss the box before box presentation has been completed, ie. before the KVO is added.
When that happens, the app crashes trying to remove a KVO that does not exist.
Is there a way to check for existence of KVO (before trying to remove)?
observationInfo
property is set when there is an observer added
if self.boxView!.observationInfo != nil {
self.boxView!.removeObserver(self, forKeyPath: "center", context: &BoxCenterContext)
}
Apple does not provide any API for check existence of observer but you can manage a Bool flag for this. Like when u register KVO you set isObserver bool to true and then before removing observer you need to check isObserver true of false if isObserver is true so remove observer and if it's false don't do any thing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With