Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios AFN网络批处理请求-第一个失败时取消_Ios_Objective C_Afnetworking - Fatal编程技术网

Ios AFN网络批处理请求-第一个失败时取消

Ios AFN网络批处理请求-第一个失败时取消,ios,objective-c,afnetworking,Ios,Objective C,Afnetworking,我正在使用AFNetworking代码来批处理请求。我真的复制并粘贴了示例代码-看起来是这样的: NSMutableArray *mutableOperations = [NSMutableArray array]; for (NSURL *fileURL in filesToUpload) { NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"PO

我正在使用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];
NSMutableArray*mutableOperations=[NSMutableArray];
for(文件中的NSURL*fileURL-toupload){
NSURLRequest*request=[[AFHTTPRequestSerializer]multipartFormRequestWithMethod:@“POST”URLString:@”http://example.com/upload“参数:nil constructingBodyWithBlock:^(id formData
[formData appendPartWithFileURL:fileURL名称:@“images[]”错误:nil];
}];
AFHTTPRequestOperation*操作=[[AFHTTPRequestOperation alloc]initWithRequest:request];
[可变操作addObject:操作];
}
NSArray*operations=[AFURLConnectionOperation BatchOfRequesToOperations:mutableOperation progressBlock:^(NSUniter numberOfFinishedOperations,NSUniter totalNumberOfOperations){
NSLog(@“%lu/lu完成”,numberOfFinishedOperations,totalNumberOfOperations);
}completionBlock:^(NSArray*操作){
NSLog(@“批量完成所有操作”);
}];
[[NSOperationQueue mainQueue]添加操作:操作等待完成:否];
现在,我想要实现的是,如果第一个操作失败,则取消该队列中的所有其他操作

我找到了一个1.0.3版本的AFHttpClient解决方案,但没有找到2.0版本的解决方案


任何提示?

创建自己的操作队列,而不是将操作添加到
[NSOperationQueue mainQueue]
。因此,在
@界面中定义队列:

@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;
然后,实例化一个队列:

@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;
然后,将网络操作添加到此队列(绕过批处理请求操作
):


别忘了让第二个操作依赖于第一个操作。否则,您可能会发现第二个操作实际上是在第一个操作之前运行的!@davidcarn他们从AFNetworking获得依赖关系。我将尝试该解决方案。如果您使用的是[AFURLConnectionOperation batchOfRequestOperations:],是的,你是对的。我完全同意。我阅读了OP的问题,要求如果第一个请求失败,则不应发出第二个请求。如果不是这样,则并行上载的性能应该更好。@GrzegorzKrukowski您可以自己将操作排队,而无需使用BatchorRequestOperations。这样,您就可以可以按照Rob的建议创建一个串行队列,并自己配置依赖项。顺便说一句,提前下载所有图像真的很重要吗?对于许多用户界面,最好采用延迟加载(例如通过AFNetworking的
UIImageView
类别)。显然,如果您必须下载所有内容,请这样做,但如果用户可能不需要所有内容,请按您的需要下载。如果您下载所有内容,可能会不必要地消耗用户的电池、数据计划、网络带宽等。我正在做的不是下载图像。我正在上载一些JSON数据,但用于sim卡我使用了AFNetworking的例子。