如何在AFNetworking使用enqueueBatchOfHTTPRequestOperations失败时重新添加操作

我有一种方法,使用AFNetworking调用enqueueBatchOfHTTPRequestOperations将文件列表从Web服务器下载到iPad。 每隔一段时间我就会得到一次失败的操作(通常是文件较大时)。 从历史上看,我单独下载了每个文件,只需再次调用操作直到某个重试次数。

我重构了代码以使用enqueueBatchOfHTTPRequestOperations。 这很好,但我不知道如何获取有关“特定”操作失败的通知,如果失败,我不知道如何将它们重新添加到队列中。

这是我用来下载“n”个文件的代码:

NSMutableArray *operationsArray = [[NSMutableArray alloc]init]; for (MINPageDefinition *currPage in [[MINPageStore sharedInstance] pageArray]) { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *serverLibraryURL = [defaults objectForKey:kRootURL]; serverLibraryURL = [serverLibraryURL stringByAppendingPathComponent:kPageDefinitionsDirectory]; serverLibraryURL = [serverLibraryURL stringByAppendingPathComponent:currPage.pageImageName]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:serverLibraryURL]]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:currPage.pageImageURL append:NO]; [operationsArray addObject:operation]; } if ([operationsArray count] > 0) { AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]]; void (^progressBlock)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) = ^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) { NSLog(@"%d proccesses completed out of %d", numberOfCompletedOperations, totalNumberOfOperations); }; void (^completionBlock)(NSArray *operations) = ^(NSArray *operations) { NSLog(@"All operations completed"); }; [client enqueueBatchOfHTTPRequestOperations:operationsArray progressBlock:progressBlock completionBlock:completionBlock]; } 

以前,我有一个执行操作的方法。 失败时,失败块将递归调用自身。 如果操作失败,如何修改此代码以重试“n”次?

检查以下代码@justLearningAgain

 [[WfServices sharedClient] GET:kGetAddress parameters:dicParam success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"RESPONSE ::: %@",responseObject); }failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; + (WfServices *)sharedClient { static WfServices * _sharedClient = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _sharedClient = [[WfServices alloc] initWithBaseURL:[NSURL URLWithString:kWFBaseURLString]]; }); return _sharedClient; } 

难道你不能像这样在该操作的故障块中再次排队单个失败的操作吗?

 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { } failure:^(AFHTTPRequestOperation *operation, NSError *error) { [[APIClient sharedClient] enqueueHTTPRequestOperation:operation]; }]; 

其中[APIClient sharedClient]是对AFHTTPClient子类的引用(单例或不单例)。

我认为AFNetworking Auto-Retry可能会有所帮助。

或者您可以在AFHTTPClient中覆盖以下方法。

考虑resumeTasksArray =您需要重新添加到队列的操作数组。

 NSMutableArray *resumeTasksArray; before @implementation AFHTTPClient - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure { void(^authFailBlock)(AFHTTPRequestOperation *opr, NSError *err) = ^(AFHTTPRequestOperation *opr, NSError *err){ [resumeTasksArray addObject:request]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ //now, queue up and execute the original task for (NSURLRequest *previousRequest in resumeTasksArray) { NSLog(@"^^^^^^^^^^^^^^^^^^^^^^^^^^^^Resume Task URL - %@", previousRequest.URL); NSMutableURLRequest *newRequest = [previousRequest mutableCopy]; [newRequest setValue:[NSString stringWithFormat:@"Bearer %@", AppSingleton.access_token] forHTTPHeaderField:@"Authorization"]; AFHTTPRequestOperation *opera10n = [[AFHTTPRequestOperation alloc]initWithRequest:newRequest]; opera10n.responseSerializer = self.responseSerializer; [opera10n setCompletionBlockWithSuccess:success failure:failure]; if (![[self.operationQueue operations] containsObject:opera10n]) { [[self operationQueue] addOperation:opera10n]; [opera10n start]; } } [resumeTasksArray removeAllObjects]; }); }; AFHTTPRequestOperation *operation = [super HTTPRequestOperationWithRequest:request success:success failure:authFailBlock]; return operation; }