title: Having trouble populating tableView using Firebase/Firestore in iOS
body: <p>This is my first time working with a cloud database and I'm looking for a little bit of guidance here as I'm relatively new to programming, and Firestore in particular.</p>
  
  <p>I'm trying to <code>get()</code> all of my document data inside of my <code>viewDidLoad</code> and store it inside of a dictionary so that I can use it later in the <code>tableView dataSource</code> methods to populate my <code>tableView</code> Sections and Rows. </p>
  
  <p>I'm working on a gym/workout log app and I inputted some dummy data for Days and Workouts Collections, so my dictionary prints out like this...</p>
  
  <pre><code>dataDict = ["Monday": ["Chest", "Arms"], "Wednsday": ["Legs", "Arms"], "Tuesday": ["Back"]]
  </code></pre>
  
  <p>But I'm having trouble using this data to populate the fields because if I try to print out the results of <code>dataDict</code> outside of the <code>dateWorkoutRequest</code> function, like inside of my <code>dataSource</code> methods, I get an empty dictionary. Is my <code>tableView.reloadData()</code> in the wrong place? Should I be using a dictionary to parse my data or is that a bad idea?</p>
  
  <p>Here is my data structure and the relevant code...</p>
  
  <pre class="lang-none prettyprint-override"><code>/users/mi9P3TrLwkQejYo3oDIu/Days/WZ3Q6LDuu1kja5Rc/Workouts/BpLGFREoJNzNQW
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/TG2BG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TG2BG.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/Ke8gB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ke8gB.png" alt="enter image description here"></a></p>
  
  <pre><code>var daysArray = [String]()
  var dayIdArray = [String]()
  var dataDict : [String:[String]] = [:]
  
  //MARK: - viewDidLoad()
  override func viewDidLoad() {
      super.viewDidLoad()
  
      vcBackgroundImg()
      navConAcc()
  
      picker.delegate = self
      picker.dataSource = self
  
      tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
      tableView.tableFooterView = UIView()
  
      Auth.auth().addStateDidChangeListener { (auth, user) in
          self.userIdRef = user!.uid
          self.colRef = Firestore.firestore().collection("/users/\(self.userIdRef)/Days")
  
          self.dateWorkoutRequest()
      }
  }
  
  func dateWorkoutRequest(){
      self.colRef.getDocuments { (snapshot, err) in
          if let err = err
          {
              print("Error getting documents: \(err)");
          }
          else {
              //Appending all Days collection documents with a field of "dow" to daysarray...
              for dayDocument in snapshot!.documents {
                  self.daysArray.append(dayDocument.data()["dow"] as? String ?? "")
                  self.dayIdArray.append(dayDocument.documentID)
  
                  Firestore.firestore().collection("/users/\(self.userIdRef)/Days/\(dayDocument.documentID)/Workouts/").getDocuments { (snapshot, err) in
                      if let err = err
                      {
                          print("Error getting documents: \(err)");
                      }
                      else {
                          //Assigning all Workouts collection documents belonging to selected \(dayDocument.documentID) to dictionary dataDict...
                          for document in snapshot!.documents {
  
                              if self.dataDict[dayDocument.data()["dow"] as? String ?? ""] == nil {
                                  self.dataDict[dayDocument.data()["dow"] as? String ?? ""] = [document.data()["workout"] as? String ?? ""]
                              } else {
                                  self.dataDict[dayDocument.data()["dow"] as? String ?? ""]?.append(document.data()["workout"] as? String ?? "")
                              }
                              print(self.dataDict)
                          }
                      }
                  }
              }
              self.dayCount =  snapshot?.count ?? 0
              self.tableView.reloadData()
          }
      }
  }
  </code></pre>