Search code examples
objective-cmacosnsapplicationnsapplication-delegate

Order of NSApplication delegate calls


I'm noticing something strange in my NSApplication delegate callbacks. When I start the app with debugger attached, I see what I expect: applicationDidFinishLaunching: is called first, then applicationDidBecomeActive:

When I run the app without the debugger, I get the calls in the order reversed: applicationDidBecomeActive: is called before applicationDidFinishLaunching:

Is there a reason for this? It makes it very confusing to account for different scenarios based on debugger vs. non-debugger.

[note: testing this is in Mavericks]


Solution

  • The relative order of those delegate methods during launch is not documented, so you should not rely on any particular order.

    If you're concerned about some initialization not having been done when -applicationDidBecomeActive: is called, then you should do that initialization in -applicationWillFinishLaunching: rather than in -applicationDidFinishLaunching:. Alternatively, you should do the initialization on demand, such as initializing a property when its value is first requested.