title: iOS dispatch_async and NSURLConnection delegate functions not being called
body: <p>I've edited this post to make it simpler to read, I think. </p>
  
  <p>I need to call an NSUrlConnection after I've finished doing some intensive string manipulation in a dispatch_async block.</p>
  
  <p>The URL I call has got .htaccess authentication on it, so I can't use a synchronous connection.</p>
  
  <p>But the NSURLConnection delegate methods are not being called. I know the URL loads after about 5 seconds in the browser, and I've tested the code with a simple URL with no authentication and it makes no difference.</p>
  
  <p>What is stopping the delegate methods being called?</p>
  
  <p>This function does some string manipulation stuff, and takes a while to finish:</p>
  
  <pre><code>- (void)performSearch {
  
      // set some defaults and work out if it is a valid search, setting bSearchOk
  
      if (bSearchOk) {
  
          // update some interface elements
          self.msgBox.text = @"Translating...";            
          self.plateInput.enabled = NO;
          self.searchProgress.hidden = NO;
  
          dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  
              // create a new search
              Search *oPlateSearch = [[Search alloc] initWithTerm:thisTerm];
  
              // ...
              // perform search... this calls a variety of slow and intensive functions
              // ...
  
              self.aFinalPlates = [oPlateSearch.aValidated copy];
              self.aCurrentPlates = [oPlateSearch.aFinals copy];        
  
              dispatch_async( dispatch_get_main_queue(), ^{                 
                  [oPlateSearch release];              
  
                  // make ajax call to check if plates can be bought
                  [self getPrices];                 
              });
          });
  
      } else {
          // hide results
          self.searchResults.hidden = YES;
      }
  
  }
  </code></pre>
  
  <p>And this is the one called towards the end of the block above</p>
  
  <pre><code>/* call to get plate availability and prices */
  -(void) getPrices {
      // set return message
      self.msgBox.text = @"Getting prices and availability";
  
      // ...
      // then I build my strRequest var, which is a valid working URL
      // ...
  
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strRequest]];
  
      NSURLConnection *loginConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
  
      NSLog('This will get logged');
  
      if(loginConnection) {
          NSLog('This will also get logged');
          self.jsonSearchResponse = [[[NSMutableData data] retain] autorelease];
          NSLog('And this will get logged, so it's not throwing errors');
      } else {
          NSLog(@"Failed to get search results");
      }
  }
  </code></pre>
  
  <p>Update: I've tried this as well, as it was an accepted in answer on another similar question, but it hasn't worked:</p>
  
  <pre><code>NSURLConnection *loginConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
  [loginConnection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
  [loginConnection start];
  </code></pre>
  
  <p>Based on this question: <a href="https://stackoverflow.com/questions/5474845/why-nsurlconnection-delegate-methods-dont-get-called-when-using-the-global-dis">Why NSURLConnection delegate methods don&#39;t get called, when using the global dispatch queue?</a></p>