title: Swift closure async order of execution
body: <p>In my model have function to fetch data which expects completion handler as parameter:</p>
  
  <pre><code>func fetchMostRecent(completion: (sortedSections: [TableItem]) -&gt; ()) {
  
          self.addressBook.loadContacts({
              (contacts: [APContact]?, error: NSError?) in
              // 1
              if let unwrappedContacts = contacts {
                  for contact in unwrappedContacts {
  
                      // handle constacts
                      ...                        
                      self.mostRecent.append(...)
                  }
              }
              // 2
              completion(sortedSections: self.mostRecent)
          })
  }
  </code></pre>
  
  <p>It's calling another function which does asynchronous loading of contacts, to which I'm forwarding my completion</p>
  
  <p>The call of <code>fetchMostRecent</code> with completion looks like this:</p>
  
  <pre><code>model.fetchMostRecent({(sortedSections: [TableItem]) in
      dispatch_async(dispatch_get_main_queue()) {
          // update some UI
          self.state = State.Loaded(sortedSections)
          self.tableView.reloadData()
      }
  })
  </code></pre>
  
  <p>This sometimes it works, but very often the order of execution is not the way as I would expect. Problem is, that sometimes <code>completion()</code> under <code>// 2</code> is executed before scope of <code>if</code> under <code>// 1</code> was finished. </p>
  
  <p>Why is that? How can I ensure that execution of <code>// 2</code> is started after <code>// 1</code>? </p>