我如何等待文件下载完成

时间:2014-06-08 20:53:54

标签: ios objective-c

我有一个应用程序必须在开始应用程序时下载配置文件,我使用NSURLConnection下载文件并进行处理。我遇到的问题是我需要等到配置下载才能继续应用。我该怎么做呢 ?

以下示例代码

- (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;
}

1 个答案:

答案 0 :(得分:0)

简短的回答是,“你没有”。原因是您在用户启动应用程序和didFinishLaunchingWithOptions返回的时间之间有大约5秒的时间。花费超过5秒钟,应用程序被看门狗定时器杀死。因此,您永远不应该在didFinishLaunchingWithOptions中进行任何形式的联网。

如果需要下载配置文件,则didFinishLaunchingWithOptions应显示“请等待下载”视图控制器,其中包含旋转活动指示器或进度条。该视图控制器可以在viewDidLoad中启动下载。下载完成后,删除该视图控制器并将其替换为导航控制器。