1

Can you help me solve this problem? I want to delete a specific message in the database.

My database looks like this:

• MESSAGES  
 ••(childByAutoID XXXXXXXXX)  
  •••email: user1@gmail.com  
  •••message: hello there  
  •••timestamp: 329842938592  
•  
 ••(childByAutoID XXXXXXXXX)  
  •••email: user1@gmail.com  
  •••message: where are you?  
  •••timestamp: 872985042750  
•  
 ••(childByAutoID XXXXXXXXX)  
  •••email: user2@gmail.com  
  •••message: basketball?  
  •••timestamp: 845938459349

I tried using this code but it deletes the wrong post from the current user (user1).

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


    if (editingStyle == .delete) {
        jobRequests.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .automatic)

        if let email = Auth.auth().currentUser?.email {

            Database.database().reference().child("MESSAGES").queryOrdered(byChild: "email").queryEqual(toValue: email).observeSingleEvent(of: .childAdded, with: { (snapshot) in

               snapshot.ref.removeValue()

so how can i delete the message "where are you?" and its members.

enter image description here

Nick
  • 104
  • 11

1 Answers1

1

The query needs to match what you want to delete.

If you want to delete the message with text "where are you?", you should query for that:

let messagesRef = Database.database().reference().child("MESSAGES")

messagesRef.queryOrdered(byChild: "message").queryEqual(toValue: "where are you?")...

If you want to delete all messages with a specific email, you should query for that:

messagesRef.queryOrdered(byChild: "email").queryEqual(toValue: "email1@gmail.com")...

If you want to delete a specific message, you must know its key and then:

messagesRef.queryOrderedByKey().queryEqual(toValue: "-L....")...

Or more idiomatic:

messagesRef.child("-L....").observeSingleEvent(of: .value, with: { (snapshot) in
    snapshot.ref.removeValue()

(Note that we're observing value here, not .childAdded, since we're no long using a query.

Or even simpler; since you already know the full path of the node to delete, you don't need to read anything:

messagesRef.child("-L....").removeValue()
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • does that mean i dont need if let email = Auth.auth().currentUser?.email anymore? – Nick Oct 07 '18 at 15:03
  • If you know the message ID, you indeed don't need to know the user ID. – Frank van Puffelen Oct 07 '18 at 15:34
  • I've added a screenshot of my tableView so you can understand better. if i dont add :if let email = Auth.auth().currentUser?.email" none of the messages gets deleted in the database. apologies for asking too much @FrankVan – Nick Oct 07 '18 at 15:37
  • If you want to delete that one specific message, you must know its key. You'll typically want to associate that key with the row in the table view, while populating the table initially. – Frank van Puffelen Oct 07 '18 at 15:44
  • is it similar to this https://stackoverflow.com/questions/44744591/how-to-look-for-a-specific-value-in-a-datasnapshot-with-firebase-cloud-functions but this is javascript right, i cant use javascript in swift – Nick Oct 07 '18 at 15:47