Friday, 28 September 2018

NSKeyValueObservation: Cannot remove an observer for the key path from object because it is not registered as an observer

I get random crashes (which I can't reproduce on devices I own) in my app with exception:

Cannot remove an observer Foundation.NSKeyValueObservation 0xaddress for the key path "readyForDisplay" from AVPlayerLayer 0xaddress because it is not registered as an observer.

This happens when I deallocate a UIView which contains AVPlayerLayer.

My init:

private var playerLayer : AVPlayerLayer { return self.layer as! AVPlayerLayer }

init(withURL url : URL) {
    ...
    self.asset = AVURLAsset(url: url)
    self.playerItem = AVPlayerItem(asset: self.asset)
    self.avPlayer = AVPlayer(playerItem: self.playerItem)
    super.init(frame: .zero)
    ...
    let avPlayerLayerIsReadyForDisplayObs = self.playerLayer.observe(\AVPlayerLayer.isReadyForDisplay, options: [.new]) { [weak self] (plLayer, change) in ... }
    self.kvoPlayerObservers = [..., avPlayerLayerIsReadyForDisplayObs, ...]
    ...
    }

My deinit where exception is thrown:

deinit {
    self.kvoPlayerObservers.forEach { $0.invalidate() }
    ...
    NotificationCenter.default.removeObserver(self)
}

According to Crashlytics it happens on iOS 11.4.1 on different iPhones.

The code leading to deinit is pretty simple:

// Some UIViewController context.
self.viewWithAVLayer?.removeFromSuperview()
self.viewWithAVLayer = nil

I would appreciate any thoughts on why this happens.

I have seen this bug but it doesn't seem to be the cause for me.



from NSKeyValueObservation: Cannot remove an observer for the key path from object because it is not registered as an observer

No comments:

Post a Comment