title: Swift firebase search is one reload behind
body: <p>I have a search function in my app which works perfectly except for the fact that it seems to be one reload behind. For example if I type in 'S' nothing will show but if I then type an 'a' after all the results for 'S' will show up. I tried adding collectionView reload at the end of the fetch function but if I do that nothing shows up period.</p>
  
  <pre><code>@objc func textFieldDidChange(_ textField: UITextField) {
      guard let text = textField.text else { return }
  
      if text.count == 0 {
          self.posts.removeAll()
          self.filteredPosts.removeAll()
      } else {
          fetchSearchedPosts(searchTerm: text)
      }
      self.collectionView.reloadData()
  }
  
  func fetchSearchedPosts(searchTerm: String) {
      self.collectionView.refreshControl?.endRefreshing()
  
      let ref = Database.database().reference().child("posts").queryOrdered(byChild: "title").queryStarting(atValue: searchTerm).queryEnding(atValue: "\(searchTerm)\u{f8ff}")
      ref.observeSingleEvent(of: .value) { (snapshot) in
  
          if !snapshot.exists() { return }
  
          guard let dictionaries = snapshot.value as? [String: Any] else { return }
          self.posts.removeAll()
          dictionaries.forEach({ (key, value) in
  
              guard let postDictionary = value as? [String: Any] else { return }
              guard let uid = postDictionary["uid"] as? String else { return }
  
              Database.fetchUserWithUID(uid: uid, completion: { (user) in
  
                  let post = Post(postId: key, user: user, dictionary: postDictionary)
  
                  let nowTimeStamp = Date().timeIntervalSince1970
                  let dateTime = post.endTimeDate
                  let timeStamp = dateTime.timeIntervalSince1970
  
                      if nowTimeStamp &lt; timeStamp {
  
                      post.id = key
                      self.posts.append(post)
  
                      } else {
                          return
                      }
              })
              self.posts.sort(by: { (post1, post2) -&gt; Bool in
                  return post1.title.compare(post2.title) == .orderedAscending
              })
          })
          self.collectionView.reloadData()
      }
  }
  </code></pre>