GifHunter111
GifHunter111

Reputation: 3

let current user delete a firebase child

I try to let the current user delete one of his posts (one of him created firebase childs), but with following code the just deletes all of him created childs... but I just want him to delete the selected child.

    Database.database().reference().child("jobs").queryOrdered(byChild: "userID").queryEqual(toValue: userID).observe(.value, with: { (snapshot) in
        if let forumPosts = snapshot.value as? [String: [String: AnyObject]] {
            for (key, _) in forumPosts  {
                Database.database().reference(withPath: "jobs").child(key).removeValue()
            }
        }
    })
    self.navigationController?.popToRootViewController(animated: true)
}

I guess I need to get the current postID of the child, but I don't know how I get it...

How I tried getting the postID:

let key = Database.database().reference().child("jobs").childByAutoId().key

UPDATE:

My Firebase Structure:

{
  "jobs" : {
    "-LZBUqFT5LqaQTaeYnv7" : {
      "addedByUser" : "user1",
      "imageDownloadURL" : "https://firebasestorage.googleapis.com/v0/b/...",
      "location" : "Olympia Ring, Berlin",
      "numberOfDislikes" : 12,
      "numberOfLikes" : 14,
      "text" : "Who needs a clean garden"
    },

The user deletes the post through a normal UIButton when it's getting pressed which is inside a normal ViewController It's build up like this:

On the personal profile page is a TableView which shows only from the user created posts. If the user now clicks on one of these cells he'll get to a DetailedVC of the selectedPost - and on this page is the button

EDIT2:

-LoZQBuYHH9CnwaZ1u4z

postID: "-LoZQ7jCAuJ6zxr9x1Fj"

Is there a way to get the original PostID?

EDIT3:

@IBAction func deleteJobPressed(_ sender: Any) {

    // Remove the post from the DB
    func deletePost(post: Job) {
        ref.child("jobs").child(post.postID!).removeValue()
    }
    self.navigationController?.popToRootViewController(animated: true)
}

func download() {
    let refJob = Database.database().reference()
    refJob.child("jobs").queryOrdered(byChild: "userID").queryEqual(toValue: "yourUserID").observe(.childAdded) { (snapshot) in
        let key = snapshot.key
        let data = snapshot.value as! [String : Any]
        let post = Job(postID: key, data: data)
        self.posts.append(post)
    }
}

Upvotes: 0

Views: 179

Answers (1)

Galo Torres Sevilla
Galo Torres Sevilla

Reputation: 1575

I am not sure how you are actually making your model but here is how I would do it.

struct Post {
    var postID : String // This will be the postID from Firebase
    //.... Any other post info

    init (postID: String, data: [String : Any]) {
        self.postID = postID
    }
}

class PostDownloder {
    var posts : [Post] = []
    let ref = Database.database().reference()

    func download() {
        let ref = Database.database().reference()
        ref.child("jobs").queryOrdered(byChild: "userID").queryEqual(toValue: "yourUserID").observe(.childAdded) { (snapshot) in
            let key = snapshot.key
            let data = snapshot.value as! [String : Any]
            let post = Post(postID: key, data: data)
            self.posts.append(post)
        }
    }
}

class PostController : UIViewController { // Or whatever you are using

    let ref = Database.database().reference()

    override func viewDidLoad() {
        super.viewDidLoad()
        //.... anything else you have
    }

    //... All your other methods

    func deletePost(post: Post) {
        ref.child("jobs").child(post.postID).removeValue()
    }

}

Upvotes: 1

Related Questions