KVO Observing AVPlayer causes App crash in iOS 13

Viewed 614

I get w weird crash related to observing AVPlayer.timeControlStatus in iOS 13. It's not happening in 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. It's called in ViewController deinit.

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

The crash occurs in following situation:

  1. Open ViewController with AVPlayer and start observation.
  2. Go back to previous 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)

Here's the callstack of the crash.

enter image description here

Looks like the AVPlayer is trying to send notification to a observer that should already be invalidated and released from memory. Did anyone have a similar issue?

1 Answers

viewWillDisappear will be better place to remove observations dude.

Related