進捗状況については、setDownloadProgressBlock
(の一部、サブクラス化AFURLConnectionOperation
されている)を参照してください。例:AFHTTPRequestOperation
[operation setDownloadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
NSLog(@"Sent %d of %d bytes, %@", totalBytesWritten, totalBytesExpectedToWrite, path);
}];
また、コードサンプルでは、を呼び出しsetCompletionBlockWithSuccess:failure:
ているので、個々の操作の完了に関するステータスが提供されます。個人的には、私の小さなテストでは、要求されたダウンロードの配列を維持し、次の3つのブロック(進行状況、成功、失敗)でダウンロードのステータスコードを次のように更新します。
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個のファイルをダウンロードする場合、2つの考慮事項があります。
AFNetworkingコードを見ると、サブクラスsetMaxConcurrentOperationCount
のを適切な数(4または5)に設定して呼び出したいと思いますが、そうではないようです。それをはるかに超えると収穫逓減が見られ、すべてのファイルが単一のサーバーからのものである場合、特定のクライアントと特定のサーバー間で実行できる同時操作の数に制約があります。operationQueue
HTTPClient
私は、Appleがセルラーネットワークを介して異常な要求を行うアプリを拒否するという逸話的な主張を読みました。https://stackoverflow.com/a/14922807/1271826を参照してください。