title: iOS Long Running Operation using GCD or NSThread?
body: <p>I am new to iOS development (I'm most familiar with Java), and I was wondering what is the best method for starting a long running thread?  This thread is going to start whenever the application is in the foreground and will stop when it goes to the background.  While it is in the foreground, it is going to poll an external device every X seconds to see if if it connected.</p>
  
  <p>NSThread is very similar to the Java thread class, which makes it very easy for me to understand.  I know that I can just <code>initWithTarget:selector:object:</code> that class and call <code>start:</code> and it'll start up the thread with the method supplied in the selector.  In there I just have a <code>while(true)</code> loop that keeps on running until I break out of it.  Here is a basic example of what I'm looking to do:</p>
  
  <pre><code>- (void)startup {
      NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(devicePoller) object:nil];
      [thread start];
  }
  
  - (void)devicePoller {
      while (self.started) {
          if (![self.device isConnected]) {
              //notify the user the device isn't connected
          }
  
          [NSThread sleepForTimeInterval:2];
      }
  }
  </code></pre>
  
  <p>However, I am seeing that people recommend using GCD because it has better performance.  I understand how to use <code>dispatch_async()</code> for executing code asynchronously, but all of the examples out there seem to be for a single long running operation, not an operation that runs the entire time the application is running.  Is there a way to do this with GCD (and should I) or something else?</p>