title: Change in firebase doesn't refresh table - Tables not displaying user nodes from arrays
body: <p>I am trying to have two arrays, used as dataSources for two tableViews. One array contains user type: macro and the other array contains user type: micro.</p>
  <p>If a user type is changed from macro to micro (and vice-versa) I want to remove the user from one table and add it to the other.</p>
  <p>Currently I am able to update the user's tag if changed in Firebase Database and have it appear in the proper array when the app is restarted. The observe function only collects it once and if it is changed, it doesn't update the table until the user quits the app and reopens it. The function I am using to observe.childChanged doesn't seem to update the arrays immediately on the   user's app unless they do what was mentioned previously.</p>
  <p>My main problem is that my tables are not displaying the users to the table. I am able to access their node and user but they are not appearing in my tables.</p>
  <p>Here is the code for my user class:</p>
  <pre><code>import UIKit
  import Firebase
  
      class User: NSObject {
          var Name: String?
          var Email: String?
          var UID: String?
          var Tag: String?
  
  init?(from snapshot: DataSnapshot) {
  
          let dictionary = snapshot.value as? [String: Any]
      
          self.Name = dictionary![&quot;Name&quot;] as? String
          self.Email = dictionary![&quot;Email&quot;] as? String
          self.UID = dictionary![&quot;UID&quot;] as? String
          self.Tag = dictionary![&quot;Tag&quot;] as? String
          
          }
  }
  </code></pre>
  <p>Here is my code for loading and populating my allUsersArray the info appends:</p>
  <pre><code>func loadAllUsersAndPopulateArray() {
      let ref = Database.database().reference().child(&quot;Users&quot;)
      ref.observeSingleEvent(of: .value, with: { snapshot in
          let allUsersSnapshot = snapshot.children.allObjects as! [DataSnapshot]
          for userSnap in allUsersSnapshot {
              let user = User(from: userSnap)
              self.allUsersArray.append(user!)
              
              self.macroUsersArray = self.allUsersArray.filter { $0.Tag == &quot;Macro&quot; }
              self.microUsersArray = self.allUsersArray.filter { $0.Tag == &quot;Micro&quot; }
  
              self.observeChangeInUserProperty()
          }
      })
  }
  </code></pre>
  <p>Here is my code for observing the change from Firebase:</p>
  <pre><code> func observeChangeInUserProperty() {
      let ref = Database.database().reference().child(&quot;Users&quot;)
      ref.observe(.childChanged, with: { snapshot in
          let key = snapshot.key
  
          let tag = snapshot.childSnapshot(forPath: &quot;Tag&quot;).value as! String // ! = never optional
          //get the user from the allUsersArray by its key
          if let user = self.allUsersArray.first(where: { $0.Tag == key }) {
              if user.Tag != tag { //if the tag changed, handle it
                  user.Tag = tag //update the allUsersArray
                  if tag == &quot;Macro&quot; { //if the new tag is Macro remove the user from the Micro array
                      if let userIndex = self.microUsersArray.firstIndex(where: { $0.Tag == key }) {
                          self.microUsersArray.remove(at: userIndex)
                          self.macroUsersArray.append(user) //add user to macro array
                      }
                  } else { //new type is micro so remove from macro array
                      if let userIndex = self.macroUsersArray.firstIndex(where: { $0.Tag == key }) {
                          self.macroUsersArray.remove(at: userIndex)
                          self.microUsersArray.append(user)
                      }
                  }
                  //reload the tableviews to reflect the changes
                  self.tableView.reloadData()
                  self.microTableView.reloadData()
  
              }
          }
      })
  }
  </code></pre>
  <p>Here is my code for the controller:</p>
  <pre><code>class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  
  var allUsersArray = [User]()
  var macroUsersArray = [User]()
  var microUsersArray = [User]()
  
  override func viewDidLoad() {
          super.viewDidLoad()
          
          tableView.delegate = self
          tableView.dataSource = self
          searchBar.delegate = self
          tableView.register(UserCell.self, forCellReuseIdentifier: networkCell)
          
          microTableView.delegate = self
          microTableView.dataSource = self
          microTableView.register(microCell.self, forCellReuseIdentifier: microCell)
          
          loadAllUsersAndPopulateArray()
          
      }
  
      func numberOfSections(in tableView: UITableView) -&gt; Int {
          // #warning Incomplete implementation, return the number of sections
          if (tableView === self.tableView) {
          
              return 1
              
            }
  
  
          else if (tableView === self.microTableView) {
              // Do something else
          
              return 1
          
          }
          
          fatalError(&quot;Invalid table&quot;)
          
      }
  
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          
          if (tableView === self.tableView) {
              
                  let cell = tableView.dequeueReusableCell(withIdentifier: networkCell, for: indexPath) as! UserCell
              
                  let user = macroUsersArray[indexPath.row]
                  cell.textLabel?.text = user.Name
                      
                      return cell
                      
                  } else if (tableView === self.microTableView) {
                      
                      let cell = tableView.dequeueReusableCell(withIdentifier: microCell, for: indexPath) as! microInfluencerCell
                  
                      let user = microUsersArray[indexPath.row]
                      cell.textLabel?.text = user.Name
                  
                          return cell
              
          } else {
              fatalError(&quot;Invalid table&quot;)
          }
      }
  
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          
          if (tableView === self.tableView) {
              return macroUsersArray.count
          }
          else if (tableView === self.microTableView) {
              return microUsersArray.count
          }
          
          else {
              fatalError(&quot;Invalid table&quot;)
          }
  
      }
  
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
          
          if (tableView === self.tableView) {
              return 72
          }
          else if (tableView === self.microTableView) {
              return 72
          }
          
          else {
              fatalError(&quot;Invalid table&quot;)
          }
      }
  
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      
      if (tableView === self.tableView) {
          
          dismiss(animated: true) {
              let userName = self.macroUsersArray[indexPath.row]
              self.showSecondViewController(user: userName)
  
              print(&quot;Dismiss completed&quot;)
          }
          
      }
      else if (tableView === self.microTableView) {
          
          dismiss(animated: true) {
              let userName = self.microUsersArray[indexPath.row]
              self.showSecondController(user: userName)
  
              print(&quot;Dismiss completed&quot;)
          }
          
      }
      
      else {
          fatalError(&quot;Invalid table&quot;)
      }
    }
  }
  
  func showSecondViewController(user: User) {
  
      let SecondViewController = SecondViewController()
      SecondViewController.user = user
      let navigationController = UINavigationController(rootViewController: SecondViewController)
      self.present(navigationController, animated: true, completion: nil)
    }
  
  func loadAllUsersAndPopulateArray() {
  let ref = Database.database().reference().child(&quot;Users&quot;)
  ref.observeSingleEvent(of: .value, with: { snapshot in
      let allUsersSnapshot = snapshot.children.allObjects as! [DataSnapshot]
      for userSnap in allUsersSnapshot {
          let user = User(from: userSnap)
          self.allUsersArray.append(user!)
  
          self.macroUsersArray = self.allUsersArray.filter { $0.Tag == &quot;Macro&quot; }
          self.microUsersArray = self.allUsersArray.filter { $0.Tag == &quot;Micro&quot; }
  
          self.observeChangeInUserProperty()
       }
     })
   }
  func observeChangeInUserProperty() {
  let ref = Database.database().reference().child(&quot;Users&quot;)
  ref.observe(.childChanged, with: { snapshot in
      let key = snapshot.key
  
      let tag = snapshot.childSnapshot(forPath: &quot;Tag&quot;).value as! String // ! = never optional
      //get the user from the allUsersArray by its key
      if let user = self.allUsersArray.first(where: { $0.Tag == key }) {
          if user.Tag != tag { //if the tag changed, handle it
              user.Tag = tag //update the allUsersArray
              if tag == &quot;Macro&quot; { //if the new tag is Macro remove the user from the Micro array
                  if let userIndex = self.microUsersArray.firstIndex(where: { $0.Tag == key }) {
                      self.microUsersArray.remove(at: userIndex)
                      self.macroUsersArray.append(user) //add user to macro array
                  }
              } else { //new type is micro so remove from macro array
                  if let userIndex = self.macroUsersArray.firstIndex(where: { $0.Tag == key }) {
                      self.macroUsersArray.remove(at: userIndex)
                      self.microUsersArray.append(user)
                  }
              }
              //reload the tableviews to reflect the changes
              self.tableView.reloadData()
              self.microTableView.reloadData()
  
          }
         }
      })
    }
  }
  </code></pre>
  <p>I am so close to getting this problem solved. Any help would be amazing.</p>
  <p>EDIT:</p>
  <pre><code>func loadAllUsersAndPopulateArray() {
      let ref = Database.database().reference().child(&quot;Users&quot;).child(&quot;Talent&quot;)
      ref.observeSingleEvent(of: .value, with: { snapshot in
          let allUsersSnapshot = snapshot.children.allObjects as! [DataSnapshot]
          for userSnap in allUsersSnapshot {
              let user = User(from: userSnap)
              self.allUsersArray.append(user!)
              self.allUsersNames.append(user!.Name!)
  
          }
  
          self.observeChangeInUserProperty()
      })
  
      self.macroUsersArray = self.allUsersArray.filter { $0.Tag == &quot;Macro&quot; }
      self.microUsersArray = self.allUsersArray.filter { $0.Tag == &quot;Micro&quot; }
      
      self.tableView.reloadData()
      self.microTableView.reloadData()
      
  }
  </code></pre>