Hello,
I'm investigating a crash where keyWindow is nil when I don't expect it to be nil.
I would like to understand when this can happen. Indeed,I could make a fix by guarding for nil values, but this would lead to an invalid state.
Context
- We want to return quickly from
application(didFinishLaunchingWithOptions:), so:
- We set a view controller as a splash screen
rootViewController=> we mark the window withmakeKeyAndVisible(). - We queue initializations asynchronously on the main queue.
=> Basically, while the app is initializing starting, we're displaying a splash screen view controller.
- When the app is done initializing, it needs to present the actual UI. A last asynchronous task on the main queue does this. We get
keyWindowfromUIApplicationto set the new view controller with the actual UI. That's where we assume that it shouldn't beniland force-unwrap it, but alas, in some instances it'snil.
Misc
- This crash only happens when app built with Xcode 13.x, running on iOS 15.
- I cannot reproduce this bug, and it has fairly little occurrence. (100s over 100000 of sessions)
- I also attached a sample crash
Questions
-
Given that I made the window "key and visible" in step 1, what could cause the window to stop becoming "key".
-
What would be the correct way to get the window to set the new root view controller ?
-
I don't really want to guard against a nil value because then it means that I wouldn't be able to set my new
rootViewControllerand the app would be stuck on the launch screen.
Thanks a lot!
Bruno