10

I'm using a NSMutableURLRequest to connect to a web site to get a JSON response. I'm doing so by generating the request, and then using NSURLConnection with a delegate of self to handle the async connection. I've implemented all the normal delegate methods:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (void)connectionDidFinishLoading:(NSURLConnection *)connection

All works perfectly with getting data back, dealing with any errors that occur, etc. However with the timeout on the NSURLRequest set to 10 seconds I don't seem to get called in those methods (I expected the didFailWithError to get called). It just sits there forever and I have no notification that it timed out.

Reading the iPhone API docs I don't see any mention of what should happen when the timeout occurs, just that you can set the timeout value. What should happen? I didn't see any other delegate methods in the API that would help. I don't have to manually check for timeouts, do I? Are timeouts only for synchronous requests?

Parrots
  • 26,658
  • 14
  • 59
  • 78
  • I concur Corey that I should get it in the didDailWithError method, but it's never getting called. Both breakpoints and logs within that method show it never being called for a timeout. – Parrots Aug 27 '09 at 15:50
  • 1
    For more context, incase it makes a difference, I am using the method talked about in the answer for http://stackoverflow.com/questions/332276/managing-multiple-asynchronous-nsurlconnection-connections to keep track of the connections I have outstanding. – Parrots Aug 27 '09 at 15:56
  • I looks like this might have been a bug in the beta SDK of 3.0, because as soon as the final was release it worked as expected. – Parrots Jan 12 '10 at 18:16

5 Answers5

12

Your timeout is received in this method:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
Corey Floyd
  • 25,929
  • 31
  • 126
  • 154
4

u will br receiving such type of iofo:

in did fail with

error: Error Domain=NSURLErrorDomain Code=-1001 UserInfo=0xed4870 "timed out"

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Sandeep Kumar
  • 1,595
  • 1
  • 12
  • 19
3

The timeout behaviour is a bit quirky, see this thread on Apple’s developer forums.

zoul
  • 102,279
  • 44
  • 260
  • 354
  • 1
    That thread is great! Thanks! Note that thread gives you a reference to https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html, which lists all the errors in the NSURLErrorDomain – David H Apr 05 '13 at 15:27
2

What I did was this:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    if (error.code == -1001){
        [self showTimeoutAlert];//My method to show dialog with timeout message.
    } else {
        [self showInvalidURLAlert];//My method to show dialog with bad URL message.
    }
}

As said before -1001 is for timeout, in other tests I got -1003 (for bad URL or no connection avaliable).

I was aiming to treat timeout so anything else I treated as bad URL.

Luchini
  • 75
  • 1
  • 7
1

Put some log statements in each method and check what they give. Whenever it times out, you definitely get a notification on the console about the timeout. I am getting that notification . Check your code and if you are still have problems, let us know.

Mat Kelly
  • 2,327
  • 7
  • 29
  • 51
Sandeep Kumar
  • 1,595
  • 1
  • 12
  • 19