title: Concurrency and NSURLConnection
body: <p>Recently I've been researching and working with <code>NSURLConnection</code> and concurrency.
  There seem to be several different approaches, and the ones I've tried (dispatch queues and operation queues) all seemed to work properly, eventually.</p>
  
  <p>One problem I encountered with concurrency and <code>NSURLConnection</code>, is the delegate methods not being called. After some research I found out the <code>NSURLConnection</code> needs to either be scheduled in the main runloop, or the <code>NSOperation</code> should be running on the main thread. In the first case I'm invoking <code>NSURLConnection</code> like this:</p>
  
  <pre><code>connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
  [connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
  [connection start];
  </code></pre>
  
  <p>And in the latter case like this:</p>
  
  <pre><code>- (void)start
  {
      if (![NSThread isMainThread])
      {
          [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
          return;
      }
      connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  }
  </code></pre>
  
  <p>The delegate methods handle everything else, and both seem to work properly.
  In the case when I was using a dispatch queue, I did the same as with the first case (schedule the <code>NSURLConnection</code> in the main runloop).</p>
  
  <p>My question is, what's the difference between these two approaches? Or are they actually the same, but just a different way of implementing them?</p>
  
  <p>The second question is, why is this necessary? I'm also using an <code>NSXMLParser</code> inside an <code>NSOperation</code>, and this doesn't seem to require a main runloop or main thread, it just works.</p>