0

iOS 8, XCode 6.3.2

I want to download multiple files serially. In the wake of the Push notification, APP will start BackgroudDownload by NSURLSessionDownloadTask. After the First BackgroudDownload process has been completed, APP want to start Second process, but Second BackgroudDownload process does not start.

Code is below

// This method is called by Push Notification

- (void)startBackgroundDownload
{
    // Session
    NSURLSessionConfiguration *configFirst = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.test.first"];
    sessionFirst = [NSURLSession sessionWithConfiguration:configFirst delegate:self delegateQueue:nil];

    NSURLSessionConfiguration *configSecond = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.test.second"];
    sessionSecond = [NSURLSession sessionWithConfiguration:configSecond delegate:self delegateQueue:nil];

    // Start First Download
    NSURLRequest *requestFirst = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://xxxxx/first.zip"]];
    NSURLSessionDownloadTask *downloadTaskFirst = [sessionFirst downloadTaskWithRequest:requestFirst];
    [downloadTaskFirst resume];
}

// Finish Download

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
    if (session == sessionFirst) {
        NSURLRequest *requestSecond = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://xxxxx/second.zip"
        NSURLSessionDownloadTask *downloadTaskSecond = [sessionSecond downloadTaskWithRequest:requestSecond];
        [downloadTaskSecond resume];
    } else if (session == sessionSecond) {
        NSLog(@"all finish");
    }
}

The First is successful, and the Second is fail (not start). I want advice to pursue the cause. Thank you for any help you can provide.

4

1 回答 1

0

下载任务被分成完美的部分,如下所示。

  1. 首先制作一组要下载的 zip 文件。
  2. 初始化会话对象
  3. 编写一种可以获取 URL 和“startDownloading”的方法
  4. 在调用解压缩该文件的委托方法(成功下载)中。删除 zip 数组的第一个对象并再次调用“startDownloading”方法及其调用,直到您的数组计数大于零

我希望你能理解我在这里要解释的内容。

于 2015-06-22T10:20:03.253 回答