如何在AFNetwork 2.0上创建队列并为完成的添加操作设置完成处理程序?

目前我有这个

ASINetworkQueue *queue = [[ASINetworkQueue alloc] init];

[queue setDelegate:self];
[queue setQueueDidFinishSelector:@selector(refeshInterface)];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[queue addOperation:request];

ASIFormDataRequest *request2 = [ASIFormDataRequest requestWithURL:url2];
[queue addOperation:request2];

[queue go]

但是我需要将其转换为AFNetwork。到目前为止,我发现的所有解决方案似乎都使​​用AFNetworkClient 2.0中不存在的AFHTTPClient。

我是AFNetwork的新手,所以一些示例将不胜感激。

谢谢!

最佳答案

你需要

+ (NSArray *)batchOfRequestOperations:(NSArray *)operations
                        progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
                      completionBlock:(void (^)(NSArray *operations))completionBlock

AFURLConnectionOperation的方法。

请看以下样本
NSMutableArray *operations = [NSMutableArray array];

for (DGSocialImage *socialImage in socialImages) {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:socialImage.url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFImageResponseSerializer new];
    [operations addObject:operation];
}
NSArray *batchOperations = [AFURLConnectionOperation batchOfRequestOperations:operations
                                                                progressBlock:NULL
                                                              completionBlock:^(NSArray *operations) {
    NSError *error;
    for (AFHTTPRequestOperation *op in operations) {
        if (op.isCancelled){
            return ;
        }
        if (op.responseObject){
            // process your responce here
        }
        if (op.error){
            error = op.error;
        }
    }
}];
[[NSOperationQueue mainQueue] addOperations:batchOperations waitUntilFinished:NO];

10-06 07:57