I have a method that downloads a list of files from a web server to an iPad using the AFNetworking call enqueueBatchOfHTTPRequestOperations. Every once in a while I get a failed operation (usually when the file is larger). Historically, I downloaded each file individually and simply called the operation again up to a certain retry count.
I've refactored the code to use enqueueBatchOfHTTPRequestOperations. This works great but I don't know how to get notifications on "specific" operation failures and I don't know how to re-add them to the queue if they have failed.
Here's the code I'm using to download "n" numbers of files:
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];
}
Before, I had a method that executed the operation. On failure, the failure block would call itself recursively. How can I modify this code to retry up to "n" numbers of times if an operation fails?
I think AFNetworking Auto-Retry might be helpful.
Or you can override following method in your AFHTTPClient.
Consider resumeTasksArray = array of operations which you need to re add to the queue.
Check Below Code @justLearningAgain
Can't you just enqueue the single failed operation again in that operation's failure block like so?
Where
[APIClient sharedClient]
is a reference to yourAFHTTPClient
subclass (singleton or not).