ios - AFNetworking 批处理请求 - 在第一个失败时取消

标签 ios objective-c afnetworking

我正在使用 AFNetworking 代码来处理请求。我确实从示例代码中复制并粘贴了 - 看起来像这样:

NSMutableArray *mutableOperations = [NSMutableArray array];
for (NSURL *fileURL in filesToUpload) {
    NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData
        [formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];
    }];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [mutableOperations addObject:operation];
}

NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:mutableOperation progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
    NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
    NSLog(@"All operations in batch complete");
}];

[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];

现在我想要实现的是,如果第一个操作失败,则取消该队列中的所有其他操作。

我用 AFHttpClient 找到了 1.0.3 的解决方案,但没有找到 2.0 的解决方案。

有什么建议吗?

最佳答案

与其将操作添加到 [NSOperationQueue mainQueue],不如创建您自己的操作队列。因此,在您的 @interface 中定义一个队列:

@property (nonatomic, strong) NSOperationQueue *networkQueue;

然后,实例化一个队列:

self.networkQueue = [[NSOperationQueue alloc] init];
self.networkQueue.name = @"com.domain.app.networkqueue";

// if you want it to be a serial queue, set maxConcurrentOperationCount to 1
//
// self.networkQueue.maxConcurrentOperationCount = 1;
// 
// if you want it to be a concurrent queue, set it to some reasonable value
//
// self.networkQueue.maxConcurrentOperationCount = 4;

然后,将您的网络操作添加到此队列(绕过 batchOfRequestOperations):

NSOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"All operations done");
}];

// NSOperation *previousOperation = nil;   // if you uncomment dependency code below, uncomment this, too

for (NSURL *fileURL in filesToUpload) {
    NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData
        [formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];
    }];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    // if you want the operations to run serially, you can also
    // make each dependent upon the prior one, as well
    //    
    // if (previousOperation)
    //     [operation addDependency:previousOperation];
    //
    // previousOperation = operation;

    [completionOperation addDependency:operation];

    [self.networkQueue addOperation:operation];
}

[self.networkQueue addOperation:completionOperation];

最后,如果你想取消操作,你可以这样做:

[self.networkQueue cancelAllOperations];

关于ios - AFNetworking 批处理请求 - 在第一个失败时取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20355692/

相关文章:

iOS 解析验证用户

objective-c - Swift 中的 _cmd 供选择器使用

ios - 如何将 AFNetworking 2.0+ 与旧的 AFNetworking 一起使用?

ios - 如何将 UIImagePicker 与带有按钮和 UIImageView 的自定义单元格一起使用?

ios - 排序数组导致错误的数据显示到单元格中

c++ - 将 C++ 库转换为 Objective-C

ios - AFNetworking-iOS在PHP打印时获取JSON

ios - AFNetworking 使用 HTTPS 而不是 HTTP

ios - Nativescript-Vue IOS 安全区域布局问题

ios - 调用 writeToFile 时防止覆盖 :