Getting continous “The operation couldn’t be compl

2019-09-16 04:41发布

问题:

I am calling POST Json webservice in recursive manner to continuously Upload/download data to service until all is completed. There could be as much as 500-1000 request. But after using for sometime i keep getting below errors. -1012 is most often i get.

Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x7ae056b0 {NSErrorFailingURLKey=https://api.XXX.com/XXX/XXX/, NSErrorFailingURLStringKey=https://api.XXX.com/XXX/XXX/}


Error: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x7ba8e5b0 {NSErrorFailingURLStringKey=, _kCFStreamErrorCodeKey=57, NSErrorFailingURLKey=, NSLocalizedDescription=The network connection was lost., _kCFStreamErrorDomainKey=1, NSUnderlyingError=0x7a6957e0 "The network connection was lost."}

I had referred to below post which saying it happens on iOS8 only but i am getting on both iOS7 and iOS8 and on both Device/Simulator and Wifi/Lan

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."

Below is how i am using in method

-(void) callService {
    //counter list variable is already initialized earlier
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:   
         [NSURL URLWithString:@"https://url.com/url"]];
    NSDictionary *params = @{@"id": counter,
                     @"data": [list objectAtIndex:counter]};
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
    path:@"http://url.com/url/url1/" parameters:params];
    AFJSONResponseSerializer *operation = [[AFJSONResponseSerializer 
           alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation 
        *operation, id responseObject) {
        //Process Data
        counter++;
        [self callService];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        counter++;
        if(error.code == -1012 || error.code == -1005) {
            [self performSelector:@selector(callService) withObject:nil 
              afterDelay:5];
        } else {
            [self callService];
        }
    }];
    [operation start];
} 

Even the connection close from server side did help in this case.

回答1:

Got the solution

Upgraded to AFNetworking 2.5.3 and now i am getting very less error code like -1005, -1012... and follow subsequent request are executed properly. Also currently using Connection close from server side. Will try to use keep alive along with this and will share my observations.

Thanks to Rob for suggesting it.