【发布时间】:2013-04-06 16:11:26
【问题描述】:
如何使用 AFNetworking 按顺序下载图像? “按顺序”指的是按顺序执行success 块。
最初我认为使用NSOperationQueue 并将每个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。
我错过了什么吗?
【问题讨论】:
标签: ios afnetworking nsoperation nsoperationqueue