开发者

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]cachePolic开发者_高级运维y: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];


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...


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.


it work for me:

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


Use %f, NSTimeInterval is floating point number.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜