0

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.

Community
  • 1
  • 1
Arun Gupta
  • 2,628
  • 1
  • 20
  • 37
  • I think there should be a certain gap between requests, otherwise server will go down. – Bista May 28 '15 at 17:35
  • what should be the interval if so? – Arun Gupta May 28 '15 at 17:47
  • I don't know...someone might help with this. – Bista May 28 '15 at 17:48
  • Its not the same request, check the params are changing. – Arun Gupta May 28 '15 at 18:01
  • Sorry. Should have looked at it better. – Tobias May 28 '15 at 18:02
  • Am I correct in assuming that this error first shows up after a consistent amount of time after the first request? And after waiting 5 seconds, do the subsequent requests start to work for a bit again, or do all subsequent requests after that first failure continue to fail? – Rob May 28 '15 at 18:45
  • The behaviour is inconsistent. It starts after 1-2 requets or goes upto 40-50 and then erroe popup but once i receive error it keeps on coming for subsequent request even though there is a delay given for executing next request. – Arun Gupta May 28 '15 at 18:48
  • Hmm. I expected it to be a fixed amount of time (tho how many requests get through in that period of time may vary) before you started getting the errors. I guess it could vary depending upon what other requests the app might have been making. I'm seeing if we can confirm whether the problem is really timeout related (like that other question) or something else. – Rob May 28 '15 at 19:03
  • BTW, I disagree with the_UB's suggestion re gap between requests. If anything, I was going to observe that your requests are going much slower than they need to because you're running them sequentially. Unless one is truly dependent on the next, run them concurrently (but not more than 4 or 5 at time). You'll find that's _much_ faster. A common solution to this is to not use `[operation start]`, but rather create your own `NSOperationQueue`, set `maxConcurrentOperationCount` to 4 or 5, and then add the ops to that queue. But let's solve the original problem first. – Rob May 28 '15 at 19:08
  • Have you tried observing these requests using something like [Charles](http://charlesproxy.com)? Sometimes looking at the details of the server's response is very illuminating. – Rob May 28 '15 at 19:09
  • 1
    Finally (and sorry to bury you in comments), I notice that you're using AFNetworking 1.x. You might want to try seeing if you can reproduce the problem with 2.x, too (assuming that more contemporary version of AFNetworking is even an option). Admittedly, I find it extremely unlikely that this would solve the problem, but I'm always wary when troubleshooting a old version of a library/tool that is no longer supported. – Rob May 28 '15 at 19:19
  • Let me try out 2.x version. Also regarding server response once i start getting the error the request wont even reach webserver so even server team cant help on it. – Arun Gupta May 29 '15 at 02:51
  • Even after setting the Connection: Close at server i am getting the same error. – Arun Gupta May 29 '15 at 06:20

1 Answers1

0

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.

Arun Gupta
  • 2,628
  • 1
  • 20
  • 37