1

Please look to the following code:

NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:<...> cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3.0]; 
<...>
[NSURLConnection sendSynchronousRequest:request returningResponse:&WSresponse error:&WSerror]

This code bellow is called from a background thread:

[self performSelectorInBackground:@selector(<...>) withObject:nil];

Sometimes sendSynchronousRequest works much more than 3.0 sec. (abount 1 minute).

  1. How to set real timeout instead not working timeoutInterval:3.0?
  2. How to add an ability to user to stop freezing NSURLConnection request by Cancel button in any moment?

Thanks a lot for help!

Dmitry
  • 14,306
  • 23
  • 105
  • 189

2 Answers2

3
  1. Note that in addition to the initializer you called, you can also call -setTimoutInterval: on an NSMutableURLRequest. The timeout value does indeed work, but any value less than 240 is ignored -- that's the minimum value respected by the iOS framework. If you want to set a lower timeout value, then your only choice is to use an asynchronous request.

  2. If you want to asynchronously cancel a request (ie, perform the request in the background and allow the foreground UI thread to issue a cancel in response to the user hitting a Cancel or Stop button), then you have to make an asynchronous URL request. There's no way to do it with a synchronous request. For example, you can't even kill a dispatch queue while it is currently executing a block.

You might want to look at ASIHTTPRequest, which wraps up some of this functionality a bit differently.

AndrewS
  • 8,196
  • 5
  • 39
  • 53
2

The timeout interval just describes the amount of time the connection can be idle at any point before it times out. From the Apple documentation:

If during a connection attempt the request remains idle for longer than the timeout interval, the request is considered to have timed out.

So if your data is retrieving any significant amount of data, then it will take longer.

If you want to have more control over how long you wait before you "stop listening" then you should use an asynchronous request. An async request will also make it so the user doesn't see a freeze while waiting?

Tim Dean
  • 8,253
  • 2
  • 32
  • 59
  • Thanks for your answer! But I want to add a button Cancel to stop request in any moment. How to do this? – Dmitry Aug 26 '11 at 18:29
  • 1
    You can call the cancel method on the NSURLConnection you made the request with (assuming you saved a reference to it). That should prevent any future callbacks from being sent to your delegate. – Tim Dean Aug 26 '11 at 18:42
  • @Tim Deam - The *no-freezing* comment is only valid if you call synchronous IO on the main thread. On a background thread synchronous IO can be preferred because of the greatly reduced code complexity. – PeyloW Aug 26 '11 at 19:23
  • @PeyloW - I agree with your comments about using the synchronous form from a background thread. Based on the original post I assumed it was being called from the main thread. – Tim Dean Aug 26 '11 at 19:48
  • This code is called from a background thread - not from the main thread. – Dmitry Aug 26 '11 at 21:20
  • @Altaveron - If the code is in a background thread then you could normally use the synchronous request. However, if you want to support cancellation then you should be using the asynchronous requests. That will also make it possible to implement a custom timeout scheme if that is what you want. – Tim Dean Aug 26 '11 at 23:03
  • Thanks for your answer. But I need to use asynchronous request from a background thread. Is it impossible? didReceiveData doesn't be called in my case. – Dmitry Aug 26 '11 at 23:24
  • It should be possible to use an async request from a background thread. – Tim Dean Aug 27 '11 at 00:16
  • @Altaveron - Please explain why you **need** to use asynchronous request when you use `performSelectorInBackground:…` that will spawn a separate thread for each request. There is nothing that could possibly be blocked, or be blocking anything else? Is it the size of the returned data that does not fit in memory, and much be processed in smaller chunks? – PeyloW Aug 27 '11 at 08:21