title: Async NSURLConnection, Concurrent NSOperation, when to use NSRunLoop?
body: <p>I'm trying to run NSURLConnection async in a secondary thread (target is iOS4), for this I have created a concurrent NSOperation, I think I'm almost there, but am not clear on the following:</p>
  
  <p>1) in iOS4 NSOperationQueue addOperation starts the operation in a new thread, because of the use of GCD, based on <a href="http://developer.apple.com/library/ios/#qa/qa1712/_index.html" rel="nofollow">Technical Q&amp;A QA1712</a>, however, my tests (simulator and iPad) show that start() is always called on the main thread, any idea, do I need a check here: if on main thread then spawn a new one?</p>
  
  <p>2) if start was actually called on a secondary thread by addOperation(), then I could start my async NSURLConnection by scheduling on the current NSRunLoop:</p>
  
  <pre><code>[self.connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  [self.connection start];
  </code></pre>
  
  <p>like the LinkedImageFetcher example <a href="http://developer.apple.com/library/mac/#samplecode/LinkedImageFetcher/Listings/Core_Code_QHTTPOperation_m.html#//apple_ref/doc/uid/DTS40010038-Core_Code_QHTTPOperation_m-DontLinkElementID_6" rel="nofollow">here</a> and I would not need to loop until completed with:</p>
  
  <pre><code>do {
      [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  } while (!isCompelted);
  </code></pre>
  
  <p>3) assuming my custom NSOperation start() is called on the main thread and 2) is correct, and I spawn a new thread in start() to call my custom main() method with: </p>
  
  <pre><code>[NSThread detachNewThreadSelector:@selector(main) toTarget:self withObject:nil];
  </code></pre>
  
  <p>In my main() I then do need to run the current thread run loop:</p>
  
  <pre><code>do {
      [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  } while (!isCompelted);
  </code></pre>
  
  <p>This is the only instance I've managed to run NSURLConnection in a concurrent NSOperation, but I'm not sure I do need to run the RunLoop, if the thread was supplied by the GCD as the tech notes state, could I follow the logic in 2) or would I still have to run the thread's runloop? How can I test a thread supplied by GCD?</p>
  
  <p>Many thanks for any elucidation</p>