Reputation: 1160
guard let uid = FIRAuth.auth()?.currentUser?.uid else {return}
let userRef = FIRDatabase.database().reference().child("user_messages").child(uid)
userRef.observe(.childAdded, with: { (snapshot) in
let messageId = snapshot.key
self.messageIdArr.append(messageId)
print(self.messageIdArr[0])
userRef.queryOrdered(byChild: "\(messageId)").queryEqual(toValue: "\(self.messageIdArr[0])").ref.removeValue()
}, withCancel: nil)
}
Hi guys, So here is my Code and here is a screenie of my Firebase structure. I'm making a snapchat clone.
Here is what I'm trying to do. Under the child user_messages
. There is a child which contains the currentuser uid and under that are the different ID for the messages. When they view the message, I was the reference to that message be deleted from Firebase under their UID.
so basically, Lets say I want to delete KcQ7kSELivNWNTAuqck
, How would I do this? My code right now, deletes everything under the UID. I just want to delete 1 messageId not all 5 under 3JEuRBs2U7QbUa7SAgppMmzQUeM2
Thanks for checking this out!
Upvotes: 2
Views: 350
Reputation: 1429
childAdded is called on each of the values that exist inside 3JEuRBs2U7QbUa7SAgppMmzQUeM2.
Meaning that if you call the removeValue inside "childAdded", (like you do now), you will eventually remove all of the ID's.
Solution: if you know the key for the user message, and the key of the message itself, the only line of code you have to write is this:
let messageToDelete = "KcQ7kSELivNWNTAuqck" //You should know how to get that
let userRef = FIRDatabase.database().reference().child("user_messages")
.child(uid).child(messageToDelete).removeValue();
Upvotes: 1