0

I realize this is a vague question, but I'm wondering if anyone else has observed this. Here is my code for calling the NSURLConnection

   // Get data from server
    NSString *host = @"www.hostname.com";            
    NSString *urlString = [NSString stringWithFormat:@"/theRestOfTheURL"];
    NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:host path:urlString];

    DLog(@"URL is %@", url);

    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.
    receivedData_ = [[NSMutableData data] retain];

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:15.0];

    // create the connection with the request
    // and start loading the data
    self.powerPlantDataConnection = [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] autorelease];

    [url release]; 

When I first load the app it works fine, and I can call it repeatedly without any problem. But if I close the app and reopen it, the

(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

delegate method gets called every time, with a request timed out error message. I have two different view controllers where I am making calls to two different URLS, and both of them fail every time after closing and reopening the app.

Can anyone think of any reason why this might be happening? I'm not sure where to start looking. What could be the cause of a request timed out error? There should be nothing wrong with the request since it works when I first run the app.


Edited to add that it seems I only have this problem on my device, not on the simulator.

Darren
  • 10,091
  • 18
  • 65
  • 108

2 Answers2

3

Wish you had shared some chrash log(especially with clear definition like [error localizedDescription] class method..)

As you have said it is going to timeout(your request). and since your way of creating the objects is too messy, you make the work bigger for your system. I suggest using GCD when downloading data and especially in situations like yours, having different interfaces and urls..

A suggestion You can create your url object like this:

   NSURL *url = [NSURL urlWithString:[NSString stringWithFormat:@"http://%@/%@?key1=%@&key2=%@", yourDomain, targetFile, value1, value2]];
ilhnctn
  • 2,210
  • 3
  • 23
  • 41
  • Thanks. There are no crash logs, because the app doesn't crash, only the NSURLConnection fails. The localized description of the error is just "Request timed out" or something similar to that. I'll see about creating my url scheme the way you suggested to see if it makes a difference. – Darren Jul 11 '12 at 17:39
  • probably it wont make a difference but it will use less memory. try GCD, if you need code&sample please write again. – ilhnctn Jul 11 '12 at 22:10
0

I switched over to using ASIHTTPRequest, which improved things but I would still get stuck in situations where I was getting timed out errors every time I tried to refresh. I looked and asked around, and eventually found that disabling calls to TestFlight solved my issue. More information here:

ASIHTTPRequest request times out

and here:

github.com/pokeb/asi-http-request/issues/320

Community
  • 1
  • 1
Darren
  • 10,091
  • 18
  • 65
  • 108