Question

I'm writting a client/server application that needs to send some XML to a server.

NSMutableArray *operations = [NSMutableArray array];
AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];

[operation1 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    float progress = (float)totalBytesRead / totalBytesExpectedToRead;
    NSLog(@"Progress 1 = %f",progress);
}];
[operations addObject:operation1];

AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];

[operation2 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    float progress = (float)totalBytesRead / totalBytesExpectedToRead;
    NSLog(@"Progress 2 = %f",progress*100);
}];
[operations addObject:operation2];






NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
// Set the max number of concurrent operations (threads)
[operationQueue setMaxConcurrentOperationCount:3];
[operationQueue addOperations:@[operation1, operation2] waitUntilFinished:NO];

What I want to do now is to handle if the transmission fails and have a queue so it retries to send it.

What's the best way to achieve this with the AFNetworking library ?

Was it helpful?

Solution

First up it would not be very wise to just retry failed operations again. Depending on what was the source of the error, you risk severe side effects like duplicate submissions.

You're already using AFHTTPRequestOperation, so the easiest solution would be to call setCompletionBlockWithSuccess:failure: and handle errors in the "failure"-block. After all you may also want to use the "success"-block for when the download successfully finished.

One last detail about the code you provided: You're creating the NSArray *operations in line 1 - yet you're not using it for anything since you create a new array of the operations in the last line. So either you left something out or you should simplify that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top