iPhone app launch time, lifecycle question
I'm trying come up with a method to determine how long after the user taps the app icon it takes for the app to be ready for user input.
As far as I know, the first place I can insert code to be run is the main() function in main.m, and the last thing that is done before user input is accepted is call applicationDidFinishLaunching in the app delegate. However, I'm finding that main() isn't called for several seconds after app launch, and can't figure out why.
Is my understanding 开发者_运维问答of the app life cycle correct? Is there some place earlier in the life cycle I can start measuring this info? Anybody have a solution?
Several things happen before main
is called:
- address space is allocated (for stack, executable, ...)
- the executable is loaded
- process is linked to dyld (the dynamic linker)
- load commands in the mach header are performed (frameworks and libraries loaded)
- more pages get allocated for static storage of libraries
- dyld performs symbol resolving
- initializations of libraries and the executable are performed
This should only name a few of the things which happen before main gets called. You can put code in one of the initialization functions to be able to do something a little before main but don’t expect this to reduce the delay between tap and your code a lot.
You can declare a C function to be called before main like this:
void __attribute__ ((constructor)) my_init(void);
If you need to do Objective-C things, you can implement +initialize
in one of your classes.
main() should be the first thing called, but there may be other things the system is doing first (loading libraries and resources, copy things around, etc). You're on the right track with a main-to-applicationDidFinishLaunch:'ing measurement.
I'm guessing that you're seeing this delay when running your application via Xcode. Xcode has to set up a few things behind the scenes to grab console output, etc., which induces an initial delay on startup.
My recommendation is to perform your application startup timings (from main() to -applicationDidFinishLaunching:) by launching the application manually on the device. Do this a few times to account for initial load artifacts. If you log your results to the console, you can then grab them using the Xcode organizer. I've seen much shorter startup timings when testing my applications this way.
Without access to the SpringBoard (something that only Apple and owners of jailbroken phones have), you can't get a very precise measurement of launch time. If your phone is jailbroken you might be able to create a debug version of of your app and run it with the time
command.
On the other hand, nearly all of the launch time that's under your control as a programmer happens between main()
and applicationDidFinishLaunching:
. So that might be a good place to start anyway.
精彩评论