Charlie
Charlie

Reputation: 169

How to remove a reference autoId value in a UItableView from Firebase tree in Swift?

I have a tableview loaded with messages from Firebase. Storing the data is easy, but trying to delete a specific node with an autoID is troubling me.

I know I have to get a reference to the first node which in this case is Posts, but how do I get the next node as a reference? Which in this case this node is an autoId. Below is the code.

 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {


        print(messages)
        print(indexPath.row)            

        var messageIndexRef = postData[indexPath.row]

        print(messageIndexRef)


        ref?.child("Posts").child("how do I get this auto ID?").removeValue(completionBlock: { (error, ref) in

            if error != nil {

                print("error \(error)")

            }
        })

       MessagesTableView.reloadData()
    }
}

Upvotes: 0

Views: 481

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599001

You will need to keep a dictionary that maps each row/index of the table view to the key of the corresponding item in Firebase.

As far as I can see from the snippet you shared, it would at the same level postData. One holds the key the other the value of each row.

If you build both dictionaries while you're reading the data from Firebase (the same moment you get the value), you can then use the dictionary with keys to determine the key of the row the user clicked on.

Upvotes: 1

Related Questions