title: How to return data in array from Firestore?
body: <p>I am building an IOS app and I am at the stage where I want to load some categories in a pickerview. The list of categories is stored in Firestore as documents.</p>
  
  <p>I have a function that does the following:</p>
  
  <ol>
  <li>Initiate an array </li>
  <li>Read data from Firestore</li>
  <li>Append the array with values</li>
  <li>Return the array</li>
  </ol>
  
  <p>The problem is that the function seems to return the array without even getting any results from Firestore</p>
  
  <p>My data model</p>
  
  <pre><code>class categoryModel {
      var catImg=""
      var catName=""
      var catID=""
      //set categories
      init(catImg: String, catName:String, catID:String){
          self.catName=catName
          self.catImg=catImg
          self.catID=catID
      }
  }
  </code></pre>
  
  <p>Firestore reference</p>
  
  <pre><code>struct FirestoreReferenceManager {
  
      static let event = "events"
  
      static let db = Firestore.firestore()
      static let rootEvents = db.collection("events")
      static let rootUsers = db.collection("users")
  
  }
  </code></pre>
  
  <p>Firestore service</p>
  
  <pre><code>class FirestoreService {
  
      static func getCategoryfromDB()-&gt;[categoryModel] {
  
          let rootCategory = FirestoreReferenceManager.rootEvents
          var tabCategory=[categoryModel]()
  
  
          rootCategory.getDocuments() { (querySnapshot, err) in
              if let err = err {
                  print("Error getting documents: \(err)")
              } else {
                  for document in querySnapshot!.documents {
  
                      tabCategory.append(categoryModel(catImg: document.data()["img"] as! String, catName:document.data()["name"] as! String, catID: document.documentID))
  
                      print("number of categories running: \(tabCategory.count)")
                  }
              }
  
          }
          print("number of categories final: \(tabCategory.count)")
          return tabCategory
      }
  }
  </code></pre>
  
  <p>Check what the log is showing</p>
  
  <pre><code>umber of categories final: 0
  number of categories final: 0
  number of categories running: 1
  number of categories running: 2
  number of categories running: 3
  number of categories running: 4
  number of categories running: 5
  number of categories running: 6
  number of categories running: 7
  number of categories running: 8
  number of categories running: 9
  number of categories running: 10
  number of categories running: 11
  number of categories running: 1
  number of categories running: 2
  number of categories running: 3
  number of categories running: 4
  number of categories running: 5
  number of categories running: 6
  number of categories running: 7
  number of categories running: 8
  number of categories running: 9
  number of categories running: 10
  number of categories running: 11
  </code></pre>
  
  <p>The 2 first lines should actually be the lasts.</p>
  
  <p>And when I read Firestore from a viewcontroller, the array is empty. I tried hardcoding the data model and it is working fine.</p>
  
  <p>I read about some "delay" in getting data, but then how to fix this?</p>
  
  <p>How do I solve the problem?</p>