使用iOS中的NSURLConnection按顺序下载文件 [英] Downloading files in serial order using NSURLConnection in iOS

查看:99
本文介绍了使用iOS中的NSURLConnection按顺序下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按顺序下载3个文件。其中两个是txt文件,一个是.gz文件。我正在使用NSURLConnection下载上述文件。

I want to download 3 files in serial order. Two of them are txt files and one is .gz file. I am using NSURLConnection to download the above files.

我是iOS编程的新手。我在SO和google的其他问题中看到我们可以使用串行调度队列来连续执行某些操作。

I am very new to iOS Programming. I have seen in other question in SO and google that we can use serial dispatch queue to do some operation serially.

但我不知道如何使用NSURLConnection执行此操作。我在下面试过但没有用。

But I don't know how to do this with NSURLConnection. I tried below but did not work.

 dispatch_queue_t serialQueue = dispatch_queue_create("com.clc.PropQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(serialQueue, ^{
    [self downloadProp];
});
dispatch_async(serialQueue, ^{
    [self downloadDatabase];
});
dispatch_async(serialQueue, ^{
    [self downloadTxt];
});

上面的代码没有执行NSURLCOnnection的connectionDidFinishLoading。任何人都有想法如何实现这一目标?

Above code is not executing connectionDidFinishLoading of NSURLCOnnection. Anyone has Idea how to achieve this?

推荐答案

NSURLSession 提供队列这将按照创建顺序下载每个任务。

NSURLSession provides a queue that will download each task in the order in which they are created.

NSURLSession *session = [NSURLSession sharedSession];

NSURLSessionTask *task1 = [session dataTaskWithURL:[NSURL URLWithString:@"http://yahoo.com"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSLog(@"Complete 1");
}];
NSURLSessionTask *task2 = [session dataTaskWithURL:[NSURL URLWithString:@"http://msn.com"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSLog(@"Complete 2");
}];
NSURLSessionTask *task3 = [session dataTaskWithURL:[NSURL URLWithString:@"http://google.com"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSLog(@"Complete 3");
}];

// Regardless of which order the tasks are "resumed" (aka started) they will execute synchronously in the order added, above.
[task3 resume];
[task1 resume];
[task2 resume];

根据评论和更新进行更新聊天:

对订购更具确定性&执行任务......

To be more deterministic over the ordering & execution of tasks...

NSURLSession *session = [NSURLSession sharedSession];

__block NSURLSessionTask *task1 = nil;
__block NSURLSessionTask *task2 = nil;
__block NSURLSessionTask *task3 = nil;

task1 = [session dataTaskWithURL:urlToFirstFile completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    // CHECK ERROR
    NSLog(@"First file completed downloading");
    [task2 resume];
}];
task2 = [session dataTaskWithURL:urlToSecondFile completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    // CHECK ERROR
    NSLog(@"Second file completed downloading");
    [task3 resume];
}];
task3 = [session dataTaskWithURL:[NSURL URLWithString:@"http://google.com"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    // CHECK ERROR
    NSLog(@"Third file completed downloading");
}];

[task1 resume];

这篇关于使用iOS中的NSURLConnection按顺序下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆