Gavin
Gavin

Reputation: 2824

NSMutableURLRequest setTimeoutInterval issue

Ok firstly if I use a NSURLReuqest(non mutable) as following, the the connection do timeout accordingly to what was set. The weird thing is why does the NSLog always read 0?

self.requestURL = [NSURLRequest requestWithURL:[NSURL URLWithString:requestString]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
NSLog(@"request timeOutInterval:%d", self.requestURL.timeoutInterval); // always 0

Next, I do something like this and the timeoutInterval does not get set.

self.requestURL = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:requestString]] autorelease];
[self.requestURL setTimeoutInterval:20];
NSLog(@"request timeOutInterval:%i", self.requestURL.timeoutInterval); // same thing always 0 here.

EDIT. I am using %f to log the timeoutInterval property now and both reads 20.000. But the real problem is why was my my NSMutableURLRequest not firing the - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error delegate call back method when it reaches the timeoutInterval(20s). Instead it is only timed out at around the 75s. Even longer than the default of 60s...

Even if I remove the [self.requestURL setTimeoutInterval:20]; line, the connection still timeout at 75s.

I have tried

self.requestURL = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:requestString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0] autorelease];

Upvotes: 1

Views: 5525

Answers (4)

visakh7
visakh7

Reputation: 26390

Try this

NSLog(@"request timeOutInterval:%f", self.requestURL.timeoutInterval); It worked for me.

UPDATE

Check this answer by @Francois in this previous SO question

There's a thread on Apple dev forums discussing this issue. Apparently on iPhone OS, the setter mandates timeoutInterval a minimum of 240 seconds (4 minutes). This only occurs when the postBody is not empty (typically when using a POST request). This seems crazy, but apparently it's there to make sure requests leave the system even though it might take many seconds for the WWAN (3G) interface to wake up. 240 seconds seems rather steep, so they suggest to set a timer and cancel the asynchronous connection when your timer fires. I know this seems stupid, but that's the only I managed to get timeout for POST requests...

Upvotes: 3

ARC
ARC

Reputation: 1804

Use %f, NSTimeInterval is floating point number.

Refer Foundation data types ref:
NSTimeInterval
Used to specify a time interval, in seconds.
typedef double NSTimeInterval;

Upvotes: 0

Rahul Juyal
Rahul Juyal

Reputation: 2144

it work for me:

NSLog(@"request timeOutInterval:%@", requestURL.timeoutInterval);

Upvotes: 0

lxt
lxt

Reputation: 31304

Your NSLog statement uses a %d format specifier, which indicates an integer, but the timeoutInterval property is a double.

Try using %f instead, which indicates you want to log a double value.

Upvotes: 0

Related Questions