我有一种方法可以使用 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”次?