如何使用 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。
我错过了什么吗?