tugce
tugce

Reputation: 661

AFNetworking retry failed operation with enqueueBatchOfHTTPRequestOperations

I'm trying to use async operation requests, but at some point operation request failed due request time out. How can I form my blocks so that, time out operations re-sent and do some operation when all operations finished failed or completed but without timeout.

I really need to figure this out, thanks a lot!

[[SDAFParseAPIClient sharedClient] enqueueBatchOfHTTPRequestOperations:pagedOperations progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {
                    NSLog(@"PAGED totalNumberOfOperations: %u  numberOfCompletedOperations: %u",totalNumberOfOperations,numberOfCompletedOperations);
                } completionBlock:^(NSArray *operations) {

                    NSMutableArray *retryops = [NSMutableArray array];

                    for(AFHTTPRequestOperation *operation in operations){
                        if(operation.error.code == -1001){
                            NSLog(@"error code %d",operation.error.code);

                            [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                                NSLog(@"comp");
//actually original completion block is needed
                            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                NSLog(@"fail");
  //actually original fail block is needed
                            }];
                            [retryops addObject:operation];
                            //Do something with the response
                        }else{
                            //Handle the failure
                        }
                    }

                    if ([retryops count] == 0) {
                        NSLog(@"all paginated operations completed");
                    }
                    else {
                        [[SDAFParseAPIClient sharedClient] enqueueBatchOfHTTPRequestOperations:retryops progressBlock:
                         ^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {

                         } completionBlock:
                         ^(NSArray *operations) {
                             NSLog(@"all retry paginated operations completed");
                         }];
                    }

Upvotes: 2

Views: 1189

Answers (1)

dbainbridge
dbainbridge

Reputation: 1202

I had same issue. I had been attempting to download 200+ files from an FTP site and the NSURLRequests passed to enqueueBatchOfHTTPRequestOperations would consistently time out after the 100th file was downloaded. I ended up dropping AFNetworking entirely since it was not needed and complicated the solution. This is partly a mixture of a couple other SO answers.

NSArray *requestsArray = // my array of [[NSMutableURLRequest alloc] initWithURL:url]'s
dispatch_queue_t downloadQueue = dispatch_queue_create("downloadQueue", NULL);
dispatch_async(downloadQueue, ^{
    NSURLResponse *response;
    NSError *requestError;
    for (NSURLRequest *request in requestsArray) {
        response = nil;
        requestError = nil;
        NSData *requestData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
        if (response == nil) {
            // Check for problems
            if (requestError != nil) {

            }
        }
        else {
            // Data was received.. continue processing
            // Make sure to do any GUI related updates in main queue/thread,
            // not directly from here
        }
    }
});

This uses a serial dispatch queue so that every synchronous NSURLRequest is performed one at a time. As each request finishes I process the data from it instead of waiting for all requests to finish. Because this is not on the main queue it will not block the GUI.

Upvotes: 3

Related Questions