title: How best to safely, efficiently run code on the main queue in Swift 3
body: <p>In objective-c, if I wanted to arrange for a block to run on the main queue, whether I was already on that queue, I could use the approach from Marco Arment's FCUtilities:</p>
  
  <p><a href="https://github.com/marcoarment/FCUtilities/blob/master/FCUtilities/FCBasics.m" rel="nofollow">https://github.com/marcoarment/FCUtilities/blob/master/FCUtilities/FCBasics.m</a></p>
  
  <p>Pre-swift 3, that approach could be reproduced as:</p>
  
  <pre><code>struct Static {
      static var onceToken: dispatch_once_t = 0
  }
  
  // If we're already on the main thread, just execute the block. Else, dispatch_async(..) it onto the main thread
  func onMainThread(block: () -&gt; ()) {
      dispatch_once(&amp;Static.onceToken) {
          dispatch_queue_set_specific(dispatch_get_main_queue(), &amp;Static.onceToken, &amp;Static.onceToken, nil)
      }
  
      if (dispatch_get_specific(&amp;Static.onceToken) == &amp;Static.onceToken) {
          block()
      } else {
          dispatch_async(dispatch_get_main_queue(), block)
      }
  }
  </code></pre>
  
  <p>In a Swift 3 world, that's no longer valid. I'm currently of the belief that the <code>dispatch_get_specific</code> test is the best/safest way to decide whether to execute or dispatch the block, but am failing to port it to Swift 3. What would a Swift 3 solution be?</p>
  
  <p><strong>Edit:</strong>
  Ok, it seems I was getting too hung up on <em>"this used to be (according to some) the way to do it, how do I port that to the new syntax?"</em> when the actual answer is far simpler : <code>DispatchQueue.main.async</code></p>