title: Handing an async call to Firestore
body: <p>I have the following method inside my database.swift file:</p>
  
  <pre><code>func GetTestData(arg: Bool, completion: ([Tweet]) -&gt; ()) {
  
          let db = Firestore.firestore()
          var tweets = [Tweet]()
  
          db.collection("tweets").getDocuments() {
              querySnapshot, error in
  
              if let error = error {
                  print("unable to retrieve documents \(error.localizedDescription)")
              } else{
                  print("Found documebts")
                  tweets = querySnapshot!.documents.flatMap({Tweet(dictionary: $0.data())})
              }
  
          }
  
          completion(tweets)
      }
  </code></pre>
  
  <p>This method connects to <code>Firestore</code> retrieve data from a given collection, convert it into an array and then passes it back, I call this function with the following (located within my table view controller):</p>
  
  <pre><code>func BlahTest() {
  
          let database = Database()
          print("going to get documents")
  
          database.GetTestData(arg: true) { (tweets) in
  
              self.tweets = tweets
  
              self.tableView.reloadData()
  
          }
  
  
          print("after calling function")        
      }
  </code></pre>
  
  <p>The issue I have is when I run this my code is out of sync, and by that I mean <code>print("after calling function")</code> is called before <code>print("Found documebts")</code> which tells me it's not waiting for the async call to <code>Firestore</code> to finish,  now I'm new to <code>iOS</code> development so would someone be willing to help me understand how I go about handling this? </p>
  
  <p>Thanks in advance.</p>