chandni
chandni

Reputation: 3

Trying to update firebase but the node value gets deleted

While running this function, the firebase takes my boolean value but somehow my node name/value is getting deleted. And when I refresh and run it in my simulator again, the data is getting deleted from the simulator but is existing in the firebase. Wondering what went wrong in my code as shown in the image

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
    {

    var x: Bool = false

    let acceptTitle = grocerys[indexPath.row].accepted  ? "Decline" : "Buy"
    let accept = UIContextualAction(style: .normal, title: acceptTitle) { (action, view, nil) in
        x = !x
        let values = ["accepted": x]
        self.databaseRef?.child(self.grocerys[indexPath.row].id).updateChildValues(values)

Upvotes: 0

Views: 144

Answers (1)

nayem
nayem

Reputation: 7605

It's happening because previously your specific node had a String value. But now you are updating that node with a Dictionary.

So what's happening here is:

Previously:

-LDH9D... : "a string"

Now:

-LDH9D... : 
     accepted : true

To insert new Dictionary type value to a key that had String type value, firebase is removing your previously added String. Because you can't add to mismatched type.

But if that node had a Dictionary previously, then this update wouldn't delete that data.

Upvotes: 1

Related Questions