nick
nick

Reputation: 2853

NSMutableURLRequest timing out after approx 15 seconds. Timeout is set at 240 seconds

I'm working on an iOS app that uses a NSMutableURLRequest and a NSURLConnection to send data to my server. I need to display the error message when it fails. My code for sending the request is shown below

const int TIMEOUT = 120;

-(void)send:(id<NSURLConnectionDelegate>)delegate
{
    bodyContents = [bodyContents stringByAppendingFormat:@"--%@--",boundry];
    NSData *data = [bodyContents dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [request setValue:[NSString stringWithFormat:@"%i",[data length]+4] forHTTPHeaderField:@"Content-Length"];
    NSLog(@"Timeout before setting custom is %f",request.timeoutInterval);
    [request setTimeoutInterval:TIMEOUT];
    NSLog(@"Timeout after setting custom is %f",request.timeoutInterval);
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:delegate];
    if(connection)
        NSLog(@"Connection good");
    else
        NSLog(@"Connection not so good");
}

//in the delegate class
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
    NSLog(@"connectionDidFinishLoading");
}

-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError *)error
{
    NSLog(@"Failed with error %@",error);
    btnCancel.title = @"Retry";
    self.navigationItem.hidesBackButton = NO;
    [self showAlertDialog:[NSString stringWithFormat:@"%@",[error localizedDescription]] :@"Error"];
}

I would expect the didFailWithError message to be called after 2 minutes of no response. Instead it's called after about 15 seconds of no response which doesn't make any sense. I looked at the documentation to check if I was setting the timeout incorrectly, the only thing I could find is that the minimum timeout for a post with a body is 240 seconds which I have come to begrudgingly accept.

The output from running the app looks like this:

2012-06-22 16:49:12.393 Media Upload[672:707] Timeout before setting custom is 240.000000

2012-06-22 16:49:12.394 Media Upload[672:707] Timeout after setting custom is 240.000000

2012-06-22 16:49:12.395 Media Upload[672:707] Connection good

2012-06-22 16:49:29.174 Media Upload[672:707] Failed with error Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." etc

I'm more than a little confused as to why it's timing out so quickly. The server is not up when the connection is created so I'm not even sure why it says Connection good instead of connection not so good, but that's a different problem (I hope) Any suggestions?

Upvotes: 1

Views: 562

Answers (1)

Chris Trahey
Chris Trahey

Reputation: 18290

So, I suspect that the answer is all in the semantics.

As for the "Connection good" logging instead of "Connection not so good", this is because init'ing a connection does not wait for it to do anything on the network. It does cause it to start right away, but not before it returns from the init method.

The more interesting (read:misleading) issue is with the timeout. I suspect that what the timeout setting really means is, "If I have established a connection, but the server just hasn't given me response bytes yet". Which is distinct from "I have been trying, but cannot connect". It makes a certain amount of sense when you think of it this way; One timeout value just for all the 'network' stuff (routing and handshakes) and another for the actual payload to be processed.

Upvotes: 2

Related Questions