ios - How to delete tableview items after they are deleted in Firebase -


my tableview updates table , adds new items in real-time when added firebase database. problem cannot delete in real-time. storing data firebase in local array, , loading array tableview.

i tried condense code bit. tried put firebase code inside removedeleteditems() function inside populatearrays() function, , put after .childadded listener, did not have luck deleting data in real-time.

override func viewdidload() {          super.viewdidload()         populatearrays()              }       func removedeleteditems() {          let databaseref = firdatabase.database().reference()         databaseref.child("users").observe(firdataeventtype.childremoved, with: { (firdatasnapshot) in              guard let emailtofind = firdatasnapshot.value as? string else { return }             (index, email) in self.usernames.enumerated() {                 if email == emailtofind {                     let indexpath = indexpath(row: index, section: 0)                     self.usernames.remove(at: index)                     self.tableview.deleterows(at: [indexpath], with: .fade)                     self.tableview.reloaddata()                 }             }         })     }   func populatearrays(){          let databaseref = firdatabase.database().reference()         databaseref.child("users").observe(firdataeventtype.childadded, with: { (firdatasnapshot) in              if let data = firdatasnapshot.value as? nsdictionary {                 if let name = data[constants.name] as? string {                                  self.usernames.append(name)                                 self.removedeleteditems()                                 self.tableview.reloaddata()                 }             }         })     }  func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {          let cell = tableview.dequeuereusablecell(withidentifier: "cell", for: indexpath)          cell.textlabel?.text = usernames[indexpath.row]                 return cell     } 

isn't observed value dictionary? , shouldn't check name rather email?

the loop find name not needed. there convenience function.

databaseref.child("users").observe(firdataeventtype.childremoved, with: { snapshot in              guard let data = snapshot.value as? [string:any],                   let nametofind = data[constants.name] as? string else { return }                 if let index = self.usernames.index(of: nametofind) {                      let indexpath = indexpath(row: index, section: 0)                      self.usernames.remove(at: index)                      self.tableview.deleterows(at: [indexpath], with: .fade)                      // don't reload table view after calling `deleterows`                  }              }         }) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -