title: dispatch_sync always scheduling a block on Main Thread
body: <p>I am executing a block using dispatch_sync and the block is executed correctly. But this block is executed on the main thread. As per the Apple Doc:</p>
  
  <blockquote>
    <p>Serial queues (also known as private dispatch queues) execute one task
    at a time in the order in which they are added to the queue. <strong>The
    currently executing task runs on a distinct thread (which can vary
    from task to task) that is managed by the dispatch queue.</strong></p>
  </blockquote>
  
  <p>which means (or what I understood) that current process that is being executed will run on a separate thread.</p>
  
  <p>Below is the code that I am using to judge what's going on. It is being called inside NSURLConnection's <strong>didReceiveData:</strong> delegate method (I know I should not do that inside the didReceiveData: delegate method - but this is just a sample to focus on dispatch_sync). Following are the different ways that I can assume as a proof of my conclusion:</p>
  
  <ol>
  <li><p>Using dispatch_sync on a <strong>Global Concurrent Queue</strong></p>
  
  <pre><code>   dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  
          if ([NSThread isMainThread]) {
              NSLog(@"Main Thread");
          }
          else
              NSLog(@"Not on Main Thread");
  
          //Some Process
      });
  </code></pre></li>
  </ol>
  
  <p>Output - </p>
  
  <pre><code>         Main Thread
           Main Thread 
           Main Thread
           // Main Thread printed till didReceiveData: gets called
  </code></pre>
  
  <hr>
  
  <ol start="2">
  <li><p>Using dispatch_sync on a self created queue using <strong>dispatch_queue_create</strong></p>
  
  <pre><code>// Create queue somewhere else like this
  dispatch_queue_t newQueue = dispatch_queue_create("WriteQueue", DISPATCH_QUEUE_SERIAL);
  
  
     dispatch_sync(newQueue, ^{
  
          if ([NSThread isMainThread]) {
              NSLog(@"Main Thread");
          }
          else
              NSLog(@"Not on Main Thread");
  
          //Some Process
      });
  </code></pre></li>
  </ol>
  
  <p>Output - </p>
  
  <pre><code>         Main Thread
           Main Thread 
           Main Thread
           // Main Thread printed till didReceiveData: gets called
  </code></pre>
  
  <hr>
  
  <p>I am a bit surprised here, block is executed always on the main thread or am I missing something. Because it seems to be going against the Apple Doc I think so. Does anyone know what this is all about?</p>
  
  <p><strong><em>Update</em></strong>: As per other discussions I understand that dispatch_sync executes a block on the same thread (most of the times), then why apple docs' statements are contradicting in some ways. Why apple says "<em>The currently executing task runs on a distinct thread (which can vary from task to task) that is managed by the dispatch queue</em>." 
  Or am I still missing something?</p>