views:

81

answers:

4

Hi! 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;
}
+1  A: 

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.

James Huddleston
The problem occurs also when I'm not using Xcode.
burki
A: 

If it is doing any sort of processing that takes a while spawn a new thread and do this work on a background thread.

metalideath
A: 

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.

abdollar
+3  A: 

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.

deanWombourne
Isn't `self.managedObjectContext` exactly the same as `[self managedObjectContext]`?
Rits
oops, silly cut-and-paste typo - see my edited answer for what I meant to put!
deanWombourne
I've tried out several things, but it looks like the large xib isn't the problem. Once i've deleted the whole UITabBarController, but the problem stayed. It's really strange.
burki
how slow is 'really slow' ? - when you run it on a device with a release build and not using xcode (i.e. pressing the icon while it's not connected to your mac) how fast is the startup in seconds?
deanWombourne
Well, the startup time is between 1 and 2 seconds, but there's no smooth animation like other apps have when they are launching. So, it takes about a second or two, and then the Default image comes immediately.
burki
1-2 seconds is about right - if there is no animation then it's not finding your Default.png correctly :(
deanWombourne
It takes about 1-2 second until the Default.png is displayed and then 1-2 more seconds until the app starts. Before the Default.png is displayed there's still the home screen displayed, and the app icon stays darkened like when you're touching it. I don't think that it isn't finding my Default.png correctly.
burki