关于进展情况,请参阅setDownloadProgressBlock http://afnetworking.github.com/AFNetworking/Classes/AFURLConnectionOperation.html#//api/name/setDownloadProgressBlock:(部分AFURLConnectionOperation
, 从中AFHTTPRequestOperation
是子类),例如:
[operation setDownloadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
NSLog(@"Sent %d of %d bytes, %@", totalBytesWritten, totalBytesExpectedToWrite, path);
}];
并且,在您的代码示例中,您正在调用setCompletionBlockWithSuccess:failure:
,以便提供有关各个操作完成情况的状态。就我个人而言,在我的小测试中,我只维护一组我请求的下载,并让这三个块(进度、成功和失败)更新我的下载的状态代码,如下所示:
for (NSInteger i = 0; i < 20; i++)
{
NSURLRequest *request = ... // set the request accordingly
// create a download object to keep track of the status of my download
DownloadObject *object = [[DownloadObject alloc] init];
download.title = [NSString stringWithFormat:@"Download %d", i];
download.status = kDownloadObjectStatusNotStarted;
[self.downloadObjects addObject:download];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
download.status = kDownloadObjectStatusDoneSucceeded;
// update my UI, for example, I have table view with one row per download
//
// [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]]
// withRowAnimation:UITableViewRowAnimationNone];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
download.status = kDownloadObjectStatusDoneFailed;
// update UI
//
// [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]]
// withRowAnimation:UITableViewRowAnimationNone];
}];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
download.totalBytesRead = totalBytesRead;
download.status = kDownloadObjectStatusInProgress;
// update UI
//
// [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]]
// withRowAnimation:UITableViewRowAnimationNone];
}];
[queue addOperation:operation];
}
您可以通过这种方式跟踪各个下载。您还可以只跟踪待处理的操作(或查询HTTPClient
对象的operationQueue
属性,然后查看operationCount
的财产)。
对于下载100个文件而言,有两个考虑因素:
我本以为你会想打电话setMaxConcurrentOperationCount
on the operationQueue
你的HTTPClient
子类,将其设置为某个合理的数字(4或5),浏览一下 AFNetworking 代码,它似乎没有这样做。我发现,如果超出此范围,回报会递减,并且如果所有文件都来自单个服务器,则给定客户端和给定服务器之间可以完成的并发操作数量会受到限制。
我读过一些传闻,称苹果将拒绝通过蜂窝网络提出特殊请求的应用程序。看https://stackoverflow.com/a/14922807/1271826 https://stackoverflow.com/a/14922807/1271826.