My app started to crash on several devices running iOS 10.x. My app is partly made of Objective-C and Swift and the culprit is:
*** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <CameraViewController 0x115e0a5f0> for the key path "queueLength" from <Connectivity 0x170469f00> because it is not registered as an observer.'
Connectivity is an Objective-C based singleton NSObject derived class which I observe from CameraViewController. I've beed adding the observer in CameraViewController viewWillAppear and remove it in CameraViewController viewWillDisappear.
App has been running fine in hundreds of devices and users but lately it has started to crash on small percentage of devices and users.
It seems that for some unknown reason either viewWillDisappear is called twice or viewWillAppear is not called at all. I'm not adding or removing observers in any place other than those two functions.
I add the observer in viewWillAppear with
[[Connectivity sharedConnectivity] addObserver:self forKeyPath:@"queueLength" options:NSKeyValueObservingOptionNew context:nil];
and remove it in viewWillDisappear with
[[Connectivity sharedConnectivity] removeObserver:self forKeyPath:@"queueLength"];
Been testing this on my device and simulator but can't make it crash.
Any ideas