NSMutableURLRequest not obeying my timeoutInterval

牧云@^-^@ 提交于 2019-12-17 07:07:57

问题


I'm POST'ing a small image, so i'd like the timeout interval to be short. If the image doesn't send in a few seconds, it's probably never going to send. For some unknown reason my NSURLConnection is never failing, no matter how short I set the timeoutInterval.

// Create the URL request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
                                 initWithURL:[NSURL URLWithString:@"http://www.tumblr.com/api/write"]
                                 cachePolicy:NSURLRequestUseProtocolCachePolicy
                                 timeoutInterval:0.00000001];

/* Populate the request, this part works fine */

[NSURLConnection connectionWithRequest:request delegate:self];

I have a breakpoint set on - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error but it's never being triggered. My images continue to be posted just fine, they're showing up on Tumblr despite the tiny timeoutInterval.


回答1:


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




回答2:


As François mentioned above, the 240 seconds seemed to be working as he described up until iOS 6 (including on 5.1). Now, this timeout appears to take on the default value of 60 seconds as expected (if you didn't explicitly set it yourself), so if you have a POST request that might have relied on the longer time inadvertently, you might need to change the timeoutInterval manually to use a higher value. I've been able to set the timeout both below and above 60 seconds for a POST so it doesn't appear that the 60 second mark represents a minimum restriction to the timeout for this type of request either.




回答3:


This issue is fixed in iOS5, So you won't be facing this problem now. And your code will work perfectly



来源:https://stackoverflow.com/questions/2736967/nsmutableurlrequest-not-obeying-my-timeoutinterval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!