This line is called when the API request received a data which needs an sync throughout the app. The error occurred when multiple API requests are sent, and
UIViewController.current() is nil. So crash occurred “Unexpectedly found nil while implicitly unwrapping an Optional… “. It’s because [UIApplication sharedApplication].keyWindow is nil.
But how keyWindow can be nil?
keyWindow is set when “makeKeyAndVisible” is called.
The view controller is entry point view controller of the storyboard.
So it was before “makeKeyAndVisible” is called
But [[[UIApplication sharedApplication] delegate] window] is set at that time. Because it’s “The window to use when presenting a storyboard.”.
->
Removed the line. The check was not needed at the first place.
Prior to 10.13, KVO would throw an exception if any observers were still registered after an autonotifying object’s -dealloc finished running. Additionally, if all observers were removed, but some were removed from another thread during dealloc, the exception would incorrectly still be thrown. This requirement has been relaxed in 10.13, subject to two conditions:
• The object must be using KVO autonotifying, rather than manually calling -will and -didChangeValueForKey: (i.e. it should not return NO from +automaticallyNotifiesObserversForKey:) • The object must not override the (private) accessors for internal KVO state
If all of these are true, any remaining observers after -dealloc returns will be cleaned up by KVO; this is also somewhat more efficient than repeatedly calling -removeObserver methods.
It says you don’t need to removeObserver before deinit. But this works only from 11.2 and crash occurs in 11.1.