didFinishLaunchingwithOptions is calling after awakeFromNib
-
26-12-2019 - |
Question
Apple doc says:
Before the initial view controller is displayed, your app delegate is called to give you a chance to configure the view controller.
But that is not actually happening.
- I created project in Xcode with
single view template
. - In
ViewController.m
I addedawakeFromNib
method and inside I justNSLog(@"VC")
. - Then in
AppDelegate
indidFinishLaunchingwithOptions
I justNSLog(@"AD")
- When I am running the project
ViewController
execute first thenAppDelegate
. - With out storyboard
didFinishLaunchingwithOptions
method always execute first and I was able to configure database and other initial stuff that all ViewControllers can use but now I am not able to do that. ViewController.m
is executing first and my database setup is indidFinishLaunchingWithOptions
.
What is going wrong?
Solution
Nothing is going wrong. awakeFromNib is called when the storyboard is loaded. The documentation states "before the initial view controller is displayed".
You are probably doing things in awakeFromNib that are not appropriate, but nobody knows without more information
The app lifecycle with storyboards is slightly different to using nibs. With nibs, you'd create and populate the initial view controller in didFinishLaunching
. With storyboards, that's done for you, before didFinishLaunching
is called. You can then access self.window.rootViewController
in that method to get hold of your initial view controller.
The point remains that awakeFromNib
is not a good place to be doing anything like responding to databases being set up or querying values. Your view hasn't been loaded yet. You should be doing these things later in the lifecycle.