Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an app that must download a configuration file upon starting the appication, im using NSURLConnection to download the file and process it. The problem i'm having is i need to wait until the configuration is downloaded before the app can continue. How do i do this ?

Sample code below

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Create a config manager

    self.configModel = [[ConfigurationModel alloc] init];

    // Read the configuration

    self.configuration = [[NSDictionary alloc] initWithContentsOfFile:[self.configModel getFilePath:@"config.plist"]];

    if(!self.configuration)
    {
        NSLog(@"Notice [ConfigurationModel] No configuration file has been found, downloading the latest");
        // need to wait until this is finished and self.configuration is not empty
        [self.configModel downloadConfigurationFile];

    }

    // Create the base view controller for the project
    MyViewController * mViewController = [[MyViewController alloc] init];

    // Set the view controller as root voor the navigation
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:mViewController];

    // Set the navigationbar non translucent
    [self.navigationController.navigationBar setTranslucent:NO];

    // Set the navigationcontroller as rootview of the window
    [self.window setRootViewController:self.navigationController];

    // Make the window visibile
    [self.window makeKeyAndVisible];

    // Return true to signal the app finished with launching
    return true;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
139 views
Welcome To Ask or Share your Answers For Others

1 Answer

The short answer is, "You don't". The reason is that you have about 5 seconds between the time the user starts the app, and the time that didFinishLaunchingWithOptions returns. Take longer than 5 seconds, and the app is killed by the watch-dog timer. Hence, you should never do networking of any kind in didFinishLaunchingWithOptions.

If the configuration file needs to be downloaded, then didFinishLaunchingWithOptions should present a "please wait for download" view controller with a spinning activity indicator, or a progress bar. That view controller can initiate the download in viewDidLoad. When the download completes, remove that view controller and replace it with the navigation controller.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...