在iOS中使用NSURLConnection按顺序下载文件
我想按顺序下载3个文件。 其中两个是txt文件,一个是.gz文件。 我正在使用NSURLConnection来下载上述文件。
我对iOS编程非常陌生。 我在SO和google的其他问题中已经看到,我们可以使用串行调度队列来进行一些串行操作。
但我不知道如何用NSURLConnection来做到这一点。 我在下面尝试,但没有工作。
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。 任何人有想法如何实现这一目标?
NSURLSession
提供了一个队列,它将按照创build顺序下载每个任务。
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];
根据评论和聊天进行更新:
为了确定任务的sorting和执行
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];
一个简单的recursion解决scheme,以确保串行操作。
func serialisedRequests(session: URLSession, requests: [URLRequest], index: Int = 0) { if index >= requests.count { return } let task = session.dataTask(with: requests[index]) { data, response, error in serialisedRequests(session: session, requests: requests, index: index+1) } task.resume() }
只需将你的NSURLSession
的HTTPMaximumConnectionsPerHost
属性设置为1,并按照你想要的顺序添加任务。
看到这个答案的更多细节: https : //stackoverflow.com/a/21018964
- 如何防止temporaryContext与migratePersistentStore同时运行
- 暂停计时器上的dispatch_source_cancel会导致EXC_BAD_INSTRUCTION
- NSInputStream停止运行,有时会抛出EXC_BAD_ACCESS
- “总是返回YES
- 大中央调度与NSThread
- 大中央调度快速堆积增长
- 如何检查dispatch_async块是否已经完成运行
- 为什么在处理主线程上的大量任务时,NSOperationQueue比GCD或者performSelectorOnMainThread更快?
- 使用Grand Central Dispatch,如何检查是否有块正在运行?