jeormehadorn
jeormehadorn

Reputation: 1

Delete Post from Firebase Database

Post Structure retrieving the data

I'm trying to delete posts from the Firebase Database. I'm having a ruff time removing values that are generated from an AutoID.

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == UITableViewCellEditingStyle.delete{
        let user = FIRAuth.auth()?.currentUser
        let uid = user?.uid
        ref?.child("tests").child(uid!)/* The Posts AutoID*/.removeValue()
        tableView.reloadData()
    }
}

Upvotes: 0

Views: 548

Answers (1)

Peter Tao
Peter Tao

Reputation: 1708

You'll need to retrieve the post IDs when the data is being fetched. For example, when fetching data just grab the post ID as well.

ref.child("tests").child(uid!).observeSingleEvent(of: .value, with: { snapshot in
     for child in snapshot.children {
         let childSnap = child as! FIRDataSnapshot
         print(childSnap.key) // SAVE THIS KEY SOMEWHERE, USE IT TO DELETE
     }
)}

Upvotes: 1

Related Questions