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

*爱你&永不变心* 提交于 2019-12-08 14:54:22

问题


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.

EDIT 1:

Additional info for posterity. On iOS 10 if I don't invalidate I get reproducible crash on deinit. On iOS 11 it works without invalidation (not checked yet if crash disappears if I don't invalidate and let observers to be deinited with my class).

EDIT 2:

Additional info for posterity: I have also found this Swift bug which might be related - SR-6795.


回答1:


After

self.kvoPlayerObservers.forEach { $0.invalidate() }

Add

self.kvoPlayerObservers.removeAll()

Also I don’t like this line:

self.kvoPlayerObservers = [..., avPlayerLayerIsReadyForDisplayObs, ...]

kvoPlayerObservers should be a Set and you should insert observers one by one as you receive them.




回答2:


I have accepted matt's answer but I want to provide more info on how I actually tackled my problem.

My deinit that doesn't crash looks like this:

if let exception = tryBlock({ // tryBlock is Obj-C exception catcher.
        self.kvoPlayerObservers.forEach { $0.invalidate() };
        self.kvoPlayerObservers.removeAll()
}) {
    remoteLoggingSolution.write(exception.description)
}
... // do other unrelated stuff

Basically I try to catch Obj-C exception if it occurs and try to log it remotely.

I have this code in production for past 2 weeks and since then I didn't receive neither crashes nor exception logs so I assume matt's proposal to add kvoPlayerObservers.removeAll() was correct (at least for my particular case).



来源:https://stackoverflow.com/questions/52479027/nskeyvalueobservation-cannot-remove-an-observer-for-the-key-path-from-object-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!