0

I am using NSURLSessionDataTask and NSMutableURLRequest. I noticed that the NSMutableURLRequest has a timeout (240 seconds I believe, which is a long time). I also read that NSURLSession has a timeout also but I am unsure of exactly what it is. My question is, will the app crash if I do not handle a timeout if it occurs? Is it necessary to handle timeouts or does the OS handle it and prevents the app from crashing, and just kills the request. If we must handle it then it would be great to get some feedback in regards to my code example;

     NSURLSession * session = [NSURLSession sharedSession];
     NSURL * url = [[NSURL alloc] initWithString:self.url];
     NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
     [request setHTTPMethod:@"POST"];
     [request addValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
     NSString * params =[NSString stringWithFormat:@"email=%@",some email];
     [request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];


     NSURLSessionDataTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

     NSHTTPURLResponse * httpResp = (NSHTTPURLResponse *)response;

     NSDictionary * dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    //some code missing

}

[task resume];
Neeku
  • 3,646
  • 8
  • 33
  • 43
DevC
  • 6,982
  • 9
  • 45
  • 80

1 Answers1

2

dataTaskWithRequest:completionHandler: has an error parameter. If a timeout occurs, you will get a non-nil error object passed in to the completion block, and you should handle that however is appropriate for your app. You need to be handling that anyway for other types of errors that may occur. The documentation on this method is pretty sparse, but I presume that you will get a nil data object if the download fails (for any reason including a timeout), so you should check for that before you try to do anything with the data.

NSURLSessionDataTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

     NSHTTPURLResponse * httpResp = (NSHTTPURLResponse *)response;
     if (data) {
         NSDictionary * dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
     }else{
         NSLog (@"%@", error);
         // do whatever to handle the error;
     }
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Thanks @rdelmar. Yes the documentation is quite limited. Do you know if the 240second timeout limit still apply? Im think of adding a timer that cancels the task after 90seconds. 240 seconds is a long time for a user to have to wait (if they will wait that long). – DevC Jul 10 '14 at 15:40
  • 1
    @DevC, I don't know about the 240 second timeout, but you shouldn't use a timer to cancel the task. You should use the NSURLRequest method, requestWithURL:cachePolicy:timeoutInterval:, to create the request you pass in to dataTaskWithRequest:completionHandler: so you can set the timeout interval. – rdelmar Jul 10 '14 at 15:52
  • Does the OS handle the timeout in this instance? or must i implement a delegate method. – DevC Jul 10 '14 at 16:13
  • 1
    @DevC, no, the time out is just one of the possible errors that you can get when a download fails. You should handle it like I showed in my answer in the completion block. – rdelmar Jul 10 '14 at 17:24
  • As I understand it, `timeoutInterval` is for time between packets, whereas `timeoutIntervalForResource` is the overall timeout. – damianesteban Sep 22 '15 at 10:04
  • timeoutIntervalForResource is not a member of the NSMutableURLRequest. – GeneCode Oct 17 '16 at 03:06