AFNetworking で画像を順番にダウンロードするにはどうすればよいですか? 「順番に」とは、success
ブロックを順番に実行することも意味します。
NSOperationQueue
最初は、 a を使用して、それぞれAFImageRequestOperation
を次の依存関係として設定するだけで十分だと思いました。このような:
- (void) downloadImages
{
{ // Reset
[_downloadQueue cancelAllOperations];
_downloadQueue = [[NSOperationQueue alloc] init];
_images = [NSMutableArray array];
}
AFImageRequestOperation *previousOperation = nil;
for (NSInteger i = 0; i < _imageURLs.count; i++) {
NSURL *URL = [_imageURLs objectAtIndex:i];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFImageRequestOperation *operation = [AFImageRequestOperation
imageRequestOperationWithRequest:request
imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[_images addObject:image];
NSLog(@"%d", i);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {}];
if (previousOperation) {
[operation addDependency:previousOperation];
}
previousOperation = operation;
[_downloadQueue addOperation:operation];
}
}
i
画像をダウンロードすると順番に印刷されます。ただし、要求が既にキャッシュされている場合、成功ブロックは順不同で処理されます。これはNSOperation
制限であり、AFNetworking ではないと思います。
何か不足していますか?