我需要在队列中下载多个文件。当我尝试下载单个文件时,它的工作原理完全正常,但是当我尝试下载多个文件时,它会在首次下载的同时开始下载,直到第一次下载完成,然后再次下载为止。我正在使用AFNetworking库扩展AFDownloadRequestOperation。以下是我的代码。请帮助我,如果有任何事情我做错了。如何在iOS中的队列中下载多个文件
NSURLRequest *request = [NSURLRequest requestWithURL:videoURL];
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if(operation.response.statusCode == 200) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Successfully Downloaded" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if(operation.response.statusCode!= 200) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Error While Downloaded" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}];
[operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
float percentDone = ((float)totalBytesRead)/totalBytesExpectedToReadForFile;
delegate.progressDone = percentDone;
[progressTableView reloadData];
}];
[operation start];
最好是检查AFDownloadRequestOperation文档,了解如何执行此操作。如果这没有帮助,则从第一个请求的成功或失败完成块开始第二个请求。为什么要按顺序进行操作,有没有特定的原因? – fishinear 2013-03-11 13:33:24
看看http://stackoverflow.com/questions/11186854/download-a-file-image-with-afnetworking-in-ios – 2013-03-11 14:13:29
我不熟悉AF *类,但如果它是真实的[NSOperation]的子类可以很简单地与[NSOperationQueue]集成。或者也许AF *类已经做到了。 – EricLeaf 2013-03-11 14:55:27