简体   繁体   中英

Firebase iOS Swift: Updating TableView After Removing A Child

I have a tableview that shows children of a reference. How can I update the tableview when a child is removed? The child can be removed from other devices and I want the tableview updates automatically. I know there is observe .childRemoved but not quit sure how to use it to update the tableview.

I used observe childRemoved. Then in the block, I emptied the array, reloaded the tableview, and called ref.observe .childAdded again.

ref.observe(.childRemoved, with: {(removedData) in 
            array.removeAll()
            tableView.reloadData()
            fetchUserRequests()
        })

By using: self.tableView.reloadData()

eg From an old project where I was playing with a sync'd list.

self.ref.child("familys").child(currentUserData.family).child("list").observe(.value, with: { (DataSnapshot) in

        if DataSnapshot.hasChildren() == false{
            print("No list")
        }

        else{
            self.list = DataSnapshot.value as! [String]
        }
        self.tableView.reloadData()
}

Everytime something below the level of "shoppinglist" updates, this observe block of code is called. self.tableView.reloadData() recalls all the functions such as

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return(list.count)  
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM