0

The first controller of my app has an avplayer, so i implemented observers which are set on viewWillAppear and are removed on viewDidDisappear.

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
     addObservers()

}

 override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated)
        avQueuePlayer.pause()
        // remove observers
        NSNotificationCenter.defaultCenter().removeObserver(self, name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationDidEnterBackgroundNotification, object: nil)
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationWillEnterForegroundNotification, object: nil)
}

When i receive a deeplink i create a new navigation stack and i then replace the current navigation controller stack with the new one :

            navigationVC.viewControllers = newNavigationVC.viewControllers

Instead of loading properly the new controller (which is happening properly if i remove the code for the observers), the app crashes with :

libc++abi.dylib: terminating with uncaught exception of type NSException

I have no clue of what is happening since xcode is not giving me any indication and the trace is in assembly so it is not helping either.

Could someone point me to the proper direction to debug this?

thibaut noah
  • 1,474
  • 2
  • 11
  • 39

2 Answers2

0

For those interested, i debuggued this with this solution :

App SIGABRTs on AppDelegate

putting

NSSetUncaughtExceptionHandler { exception in
print(exception)
print(exception.callStackSymbols)
}

before the return of didFinishLaunchingWithOptions

Which gave me a trace containing :

An instance 0x17000d9d0 of class AVPlayerItem was deallocated while key value observers were still registered with it.

There was a missing observer in the removal, putting :

            self.avQueuePlayer.currentItem?.removeObserver(self, forKeyPath: "status")

fixed my issue.

Community
  • 1
  • 1
thibaut noah
  • 1,474
  • 2
  • 11
  • 39
0

Often times it makes more sense to remove all observers at once.

NotificationCenter.default.removeObserver(self)
Chase McClure
  • 21
  • 1
  • 3
  • It may, but the issue on point was finding what caused the crash. Once you know what is causing the app to crash, fixing it is most of the times a piece of cake. – thibaut noah Apr 05 '17 at 15:57