title: Why can't we use a dispatch_sync on the current queue?
body: <p>I ran into a scenario where I had a delegate callback which could occur on either the main thread or another thread, and I wouldn't know which until runtime (using <code>StoreKit.framework</code>).</p>
  
  <p>I also had UI code that I needed to update in that callback which needed to happen before the function executed, so my initial thought was to have a function like this:</p>
  
  <pre><code>-(void) someDelegateCallback:(id) sender
  {
      dispatch_sync(dispatch_get_main_queue(), ^{
          // ui update code here
      });
  
      // code here that depends upon the UI getting updated
  }
  </code></pre>
  
  <p>That works great, when it is executed on the background thread. However, when executed on the main thread, the program comes to a deadlock.</p>
  
  <p>That alone seems interesting to me, if I read the docs for <code>dispatch_sync</code> right, then I would expect it to just execute the block outright, not worrying about scheduling it into the runloop, as said <a href="https://developer.apple.com/library/mac/documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html#//apple_ref/c/func/dispatch_sync">here</a>:</p>
  
  <blockquote>
    <p>As an optimization, this function invokes the block on the current thread when possible.</p>
  </blockquote>
  
  <p>But, that's not too big of a deal, it simply means a bit more typing, which lead me to this approach:</p>
  
  <pre><code>-(void) someDelegateCallBack:(id) sender
  {
      dispatch_block_t onMain = ^{
          // update UI code here
      };
  
      if (dispatch_get_current_queue() == dispatch_get_main_queue())
         onMain();
      else
         dispatch_sync(dispatch_get_main_queue(), onMain);
  }
  </code></pre>
  
  <p>However, this seems a bit backwards. Was this a bug in the making of GCD, or is there something that I am missing in the docs?</p>