Reload Data in Uitableview using Firebase

Asked

Viewed 46 times

1

I’m using a custom table that retrieves data from Firebase. But it only updates, new data, if I close the app and open again, even if entering the reloadData() on the table.

Example: The table is without any element, when a new element is added by another Viewcontroller, I go back to check the table, and this new element appears. When the second element is added, the table appears duplicated (twice this second element), but if I check in Firebase, os nós are not duplicated. So I need to close the application, and right away with it open the elements are updated in the table, it is as if the reloadData was not being recharging.

What I need to do or what I’m doing wrong?

 func exibeAtrasado(){

     tvAtrasados.register(UINib(nibName: "OnAtrasados", bundle: nil), forCellReuseIdentifier: "OnAtrasados")

     let database = Database.database().reference().child("OAtrasado")

     database.observe(DataEventType.value) { (snapshot) in

         if snapshot.childrenCount > 0 {

             for onAtrasado in snapshot.children.allObjects as! [DataSnapshot] {

                 let onsObject = onAtrasado.value as? [String: AnyObject]
                 let data = onsObject?["data"]
                 let horario = onsObject?["horário"]
                 let linha = onsObject?["linha"]
let onibAtr = ConfiguracaoOnAtrasados(horario: horario as! String, linha: linha as! String, data: data as! String)

                 self.onb.append(onibAtr)


             }

                 self.tvAtrasados.reloadData()

}

2 answers

0

I used the method childRemoved of Firebase to listen to the node I delete. I also used a UIRefreshControl() so that the user can update the dados da tableView.

     var refreshTable = UIRefreshControl()
         @objc func refresh(_ sender: Any){

                    self.tabeladeDados.reloadData()
                    self.refreshTable.endRefreshing()

            } 
         database.observe(DataEventType.childRemoved, with: { (snapshot) in {

var indice = 0
            for i in self.dados {
                if i.uuid == snapshot.key {
                    self.dados.remove(at: indice)
                    print(i.uuid)
                 self.refreshTable.attributedTitle = NSAttributedString(string: "Atualizando...")
                        self.refreshTable.addTarget(self, action: #selector(self.refresh), for: .valueChanged)
                        self.tabeladeDados.addSubview(self.refreshTable)
       }
    }

0

I was able to correct the mistake of duplicity. But the error still persists in the issue of updating. If I make the exclusion do nó, right in the Firebase a the elements still continue in the table. I need to close the app in order to update the table

Ancient

database.observe(DataEventType.value) { (snapshot)

New

  database.observe(DataEventType.childAdded, with: { (snapshot) in

Ancient

for onAtrasado in snapshot.children.allObjects as! [DataSnapshot] {

New

let onAtrasado = snapshot.value as? NSDictionary

Ancient

   let onsObject = onAtrasado.value as? [String: AnyObject]
                 let data = onsObject?["data"]
                 let horario = onsObject?["horário"]
                 let linha = onsObject?["linha"]

New

         let data = onAtrasado?["data"]
         let horario = onAtrasado?["horário"]
         let linha = onAtrasado?["linha"]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.