简体   繁体   English

KVO 观察 AVPlayer 导致 iOS 13 中的应用程序崩溃

[英]KVO Observing AVPlayer causes App crash in iOS 13

I get w weird crash related to observing AVPlayer.timeControlStatus in iOS 13. It's not happening in iOS 12.我遇到了与在 iOS 13 中观察AVPlayer.timeControlStatus相关的奇怪崩溃。在 iOS 12 中没有发生这种情况。

Here's the code for setting the observer up:这是设置观察者的代码:

// stored in View Controller 
private var playerStateObservation: NSKeyValueObservation?
@objc var player : AVPlayer?

// setting KVO after initialising AVPLayer
playerStateObservation = observe(\.player?.timeControlStatus) { [weak self] (object, change) in
    let playing = self?.player?.timeControlStatus == .playing
    self?.showPlayIcon(playing)
}

And here the function to stop observation.而这里 function 停止观察。 It's called in ViewController deinit .它在 ViewController deinit中调用。

func cleanUpObserver() {
    playerStateObservation?.invalidate()
    playerStateObservation = nil
}

The crash occurs in following situation:崩溃发生在以下情况:

  1. Open ViewController with AVPlayer and start observation.用 AVPlayer 打开 ViewController 并开始观察。
  2. Go back to previous ViewController. Go 回到之前的 ViewController。
  3. Dismiss the app to the background.将应用程序关闭到后台。
  4. Bring back the app to the foreground.将应用程序带回前台。
  5. Crash: Thread 1: EXC_BAD_ACCESS (code=1, address=0x2b1bc593c)崩溃: Thread 1: EXC_BAD_ACCESS (code=1, address=0x2b1bc593c)

Here's the callstack of the crash.这是崩溃的调用堆栈。

在此处输入图像描述

Looks like the AVPlayer is trying to send notification to a observer that should already be invalidated and released from memory.看起来 AVPlayer 正在尝试向观察者发送通知,该观察者应该已经失效并从 memory 释放。 Did anyone have a similar issue?有没有人有类似的问题?

viewWillDisappear will be better place to remove observations dude. viewWillDisappear将是删除观察的更好地方,伙计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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