title: IOS Iphone app randomly hangs indefinitely
body: <p>App being built on Xcode 4, for a base IOS level of 4 and above, tested on a variety of IPhones. Info in app is loaded from a JSON feed.</p>
  
  <p>In my attempts to track down what's happening I've repeatedly debugged through this app, a fairly basic one that has a few views, overall nothing amazing or new in it as far as apps go.</p>
  
  <p>The problem manifests itself occasionally, a lot of the time in our testing it has been if we've turned off Wifi to test on a cellular connection the app will hang when loading. </p>
  
  <p>What happens is that the splash screen appears and the white spinning activity wheel spins as if it's loading the ad (an ad is displayed as part of the overall loading process) but it just sits there spinning.</p>
  
  <p>So, most obvious question, is the ad the problem? Is it large or loading slowly? No, the JSOn feed is speedy (we check every time the app hangs and inbetween times as well to make sure it's all loading ok) and the ad is about 20k.</p>
  
  <p>Debugging gets as far as the didFinishLaunchingWithOptions method which I'll list below, it reaches the end of that method and the debugger essentially reports nothing else. It just sits there, nothing visible in the XCode debug navigator, no break points hit. </p>
  
  <p>If step through that method, once it gets to the end it goes about 6-7 steps into compiled code then simply does the same thing. </p>
  
  <p>Here's the code from that method:</p>
  
  <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions { 
  
      [self loadData];
  
      splashViewController = [[OurSplashViewController alloc] initWithNibName:nil bundle:nil];
      splashViewController.delegate = self;
  
      OurWebViewController *webViewController = (OurWebViewController *)[[[tabBarController viewControllers] objectAtIndex:2] topViewController];
      webViewController.url = [NSURL URLWithString:@"http://m.testURL.com/this_app/"];
          webViewController.path = @"/MyPath/Sample";
  
      [window addSubview:splashViewController.view];
          [window makeKeyAndVisible];
  
      return YES;
  }
  </code></pre>
  
  <p>So, it gets to the 'YES' of that method and then the debugger sails off into the land of ones and zeroes, reporting nothing back and hanging the app.</p>
  
  <p>Has anyone got any idea what could be causing this?</p>
  
  <p>UPDATE: </p>
  
  <p>Seem to have isolated that the hanging comes down to a JSON call that is sent but no response is received. So my code should handle that after a while.</p>
  
  <p>Here's the JSON code, maybe someone can notice something I'm missing in there:</p>
  
  <pre><code>- (void)fetchWithURL:(NSURL *)URL {
       if (!URL) {
          return;
      }
  
      self.jsonURL = URL;
  
      NSMutableURLRequest *request = [[NSMutableURLRequest alloc]     initWithURL:self.jsonURL];
      if (timeout) {
          [request setTimeoutInterval:timeout];
      }
      [request addValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];   
  
      connection_ = [[NSURLConnection alloc] initWithRequest:request 
                                                delegate:self 
                                        startImmediately:YES];
      [request release];
  
      if (connection_ != nil) {
          if ([self.delegate     respondsToSelector:@selector(JSONFetchWillBeginDownload:)]) {
              [self.delegate JSONFetchWillBeginDownload:self];
          }
          receivedData_ = [[NSMutableData alloc] initWithCapacity:0];
      } else {
          if ([self.delegate respondsToSelector:@selector(JSONFetch:downloadDidFailWithError:)]) {
              [self.delegate JSONFetch:self downloadDidFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorUnknown userInfo:nil]];
          }
      }
  }
  </code></pre>
  
  <p>According to Charles the reason for the failed response was:</p>
  
  <p>Client closed connection before receiving entire response</p>
  
  <p>I wonder is my timeout period too short?</p>