在我的应用程序中,我从 Web 服务获取 JSON。这个 JSON 包含我想下载的几个文件的 url。
我想一个一个地下载每个文件(等待第一个下载完成,直到第二个开始,等等)使用NSURLSessionDownloadTask
. 我还想跟踪写入的总字节数,以便更新 UI。
非常感谢提前!
在我的应用程序中,我从 Web 服务获取 JSON。这个 JSON 包含我想下载的几个文件的 url。
我想一个一个地下载每个文件(等待第一个下载完成,直到第二个开始,等等)使用NSURLSessionDownloadTask
. 我还想跟踪写入的总字节数,以便更新 UI。
非常感谢提前!
众所周知,NSURLSessionDownloadTask 不能很好地与 NSOperationQueues 配合使用,这与它们的对应物 NSURLConnection 不同(它可以封装在 NSOperation 中)。
一种选择是将所有 url 添加到一个数组中,然后在任务的 completionHandler 中,简单地将下一个项目排队。
因此,您可以在循环中创建任务,在每个任务完成处理程序中调用 progressBlock,将任务存储在数组中,然后在每个任务完成处理程序中排队下一个任务:
- (void)addRequestsWithURLs:(NSArray *)urls
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations, NSURLSessionDownloadTask *task,NSURL *location, NSURLResponse *response, NSError *error))progressBlock {
__block NSUInteger numberOfFinishedOperations = 0;
NSUInteger totalNumberOfOperations = [urls count];
for (NSString *url in urls) {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
__block NSURLSessionDownloadTask *task = [self.session downloadTaskWithRequest:request
completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
//downloadFileSomewhere
++numberOfFinishedOperations;
if (progressBlock) {
progressBlock(numberOfFinishedOperations, totalNumberOfOperations,task,destination != nil ? [NSURL fileURLWithPath:destination] : nil,response,error);
}
//queueNext
[self processCompletedTask:task];
}];
//stores an array of NSURLSessionTasks
[self.tasksWaitingToBeQueued addObject:task];
}
}
- (void)processCompletedTask:(NSURLSessionTask *)completedTask {
//clean up and queue next one
[self.tasksWaitingToBeQueued removeObject:completedTask];
nextTask = [self.tasksWaitingToBeQueued firstObject];
if (nextTask) {
[nextTask resume];
}
}
笔记
在此示例中,我将进度显示为完成的任务数而不是字节数,这是推荐的方法(也更简单)。要使用字节指示进度,您需要事先知道要下载的字节总数(因为您想显示进度条),还需要实现 NSURLSession 委托并监控每个任务的进度,捕获下载的字节并更新您的块. 如果您的服务器没有告诉您总字节数,那么您可能需要对每个资源进行 HEAD 请求并汇总大小。就个人而言,这种解决方案对于通过将进度指示为下载的文件数来简单地解决的问题来说是一种复杂的方式。
要实现这一点,可能看起来像这样:
- (void)URLSession:(NSURLSession *)session
downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
self.totalBytesWritten += totalBytesWritten;
NSUInteger totalProgressSoFar = self.totalBytesWritten;
NSUInteger totalExpectedBytes = self.totalExpectedBytes;
//you would need to capture some progress block locally - beware of retain cycles
self.progressBlock(totalProgressSoFar/totalExpectedBytes)
}
完成后,您应该将 progressBlock 设置为 nil 以防止任何保留周期。