简体   繁体   中英

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.

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 deinit ed with my class).

EDIT 2:

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

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM