Slow app launch
My iphone app is launching very slowly, and I have no idea why. My application:didFinishLaunchingWithOptions:
isn't really heavy, I'm just setting the managedObjectContext for each of my five view controllers of my tab bar controller.
Does anybody have an idea what causes the slow launch? Thanks.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
mathRootViewController.managedObjectContext = self.managedObjectContext;
favoriteRootViewController.managedObjectContext = self.managedObjectContext;
chemistryRootViewController.managedObjectContext = self.managedObjectContext;
physicsRootViewController.managedObjectContext = self.managedObjectContext;
shareRootViewController.managedObjectContext = self.managedObjectContext;
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;开发者_如何学JAVA
}
It looks like you have a very large initial xib file that's read and parsed on startup to populate mathRootViewController etc.
Try waiting until you controllers are needed before loading them i.e. put them in a seperate xib file and add methods that look a little like this
- (UIViewController *)mathRootViewController {
if (nil === mathRootViewController) {
mathViewController = [[MathViewController alloc] initWithNibName:@"MathViewController" bundle:nil];
[mathViewController setManagedObjectContext:[self managedObjectContext]];
}
return mathRootViewController;
}
and each time you use the controller don't just use mathRootViewController ;
, use [self mathRootViewController ]
instead - this pattern will wait until the first time you need the view controller to create it.
Are you running the app on your iPhone through Xcode? Apps tend to launch very slowly when run that way. Try launching the app on the iPhone itself, without using Xcode.
First I would confirm your assumption that this really is the function that is slow - use the profiler - Instruments - CPU Sampler - to see what timing information that function shows and compare it with others as something else could be slowing things down.
Once you have confirmed your assumptions and you need more details you could add very fine grained timing using "mach_absolute_time". Report time differences at the end with NSlog. Don't do too much logging as that can hurt performance as well.
So, do you just suspect that the above code is what's slow? I.e, have you added NSLog calls to see when didFinishLaunchingWithOptions gets entered, and when it returns? Just adding a few NSLog between a few of above lines will quickly show you where it gets stuck, don't you think?
Also, be aware that the init... functions get called first, and +initialize in other files, too. Add NSLogs there to see if they get called before didFinishLaunchingWithOptions to spot if any of them wastes the time.
If it is doing any sort of processing that takes a while spawn a new thread and do this work on a background thread.
Try the App without the whole managedObjectContext and see if it starts faster. If so then set the managedObjectContext only when the view opens in which you need it. If the starting time stays the same then it should be something with you nib file.
Launch the application with the DYLD_PRINT_STATISTICS
environment variable set to 1
on the run phase of your app slow target to get a print out of what's slow in the start up process. The print out will look something like:
Total pre-main time: 2.2 seconds (100.0%)
dylib loading time: 1.4 seconds (64.5%)
rebase/binding time: 205.99 milliseconds (9.2%)
ObjC setup time: 84.90 milliseconds (3.8%)
initializer time: 496.64 milliseconds (22.3%)
slowest intializers :
libSystem.B.dylib : 21.82 milliseconds (0.9%)
libglInterpose.dylib : 156.49 milliseconds (7.0%)
libMTLInterpose.dylib : 47.01 milliseconds (2.1%)
AppName : 224.40 milliseconds (10.0%)
Apple recommends 400 milliseconds.
Source: Use Your Loaf
精彩评论