How to delete an element from array from firebase? I have two arrays - the first one with names and the second one with menu type. With my code, I deleted just from the first array.
func reloadTableView() {
guestList.removeAll()
if let tableId = tableData?.id {
Database.database().reference().child("userInfo").child(uid!).child("tables").child(tableId).child("guestsOnTable").queryOrderedByKey().observe(.value) { (snapshot) in
if snapshot.childrenCount>0 {
self.guestList.removeAll()
for guest in snapshot.children.allObjects as![DataSnapshot]{
if let guestObject = guest.value as? String{
self.guestList.append(guestObject)
}
}
DispatchQueue.main.async {
self.guestsOnTableTableView.reloadData()
}
}
}
}
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let guestName = guestList[indexPath.row]
if let tableId = tableData?.id{
refGuests = Database.database().reference().child("userInfo").child(uid!).child("tables").child(tableId).child("guestsOnTable")
refGuests.observe(.value) { (snapshot, error) in
if let error = error {
print(error.description)
}
if let guests = snapshot.children.allObjects as? [DataSnapshot] {
for (index,guest) in guests.enumerated() {
if let guestObject = guest.value as? String {
if guestObject == guestName {
self.guestList.remove(at: index)
self.refGuests.setValue(self.guestList)
DispatchQueue.main.async {
self.guestsOnTableTableView.reloadData()
}
}
}
}
}
}
}
}
}
Here is the shot from firebase.