Tarvo Mäesepp Tarvo Mäesepp - 1 month ago 17
Swift Question

Unrecognized selector while removing all user posts from Firebase

I tried to delete all posts that belongs to specific user, however it crashes telling me error:


'-[FIRDataSnapshot removeValue]: unrecognized selector sent to
instance 0x174238c20'


but if I try to
po
the child, it prints out the child.

That is how I am doing it in Swift3:

FIRDatabase.database().reference().child("posts").queryEqual(toValue: self.currentUser.generalDetails.userName).ref.observe(.value, with: { (snapshot: FIRDataSnapshot!) in
for child in snapshot.children {
(child as AnyObject).removeValue() //This line gives the error.
}
})


That is the child that shold be removed if
"username":"currentUser.generalDetails.userName"
:

posts
-KUaMd3YgJlQvv_P-kdK//This has to be deletod with all its children
content:
likes:
postId:
postImageStringUrl: close
profileImageUrl:
timestamp:
username:


Wha should cause the crash?

Edit: I updated code like this which says that I have to :


Consider adding ".indexOn": "posts/username" at / to your security
rules for better performance


however I have pretty ok security rules:

{
"rules": {
".read": "auth != null",
".write": "auth != null",
"Snuses": {
".indexOn": ["Brand", "posts/username"]
}
}
}
FIRDatabase.database().reference().queryOrdered(byChild: "posts/username").queryEqual(toValue: currentUser.generalDetails.userName)
.observeSingleEvent(of: .value, with: { snapshot in

if ( snapshot.value is NSNull ) {
print("not found")
} else {
for child in (snapshot.children) {
let element = child as! FIRDataSnapshot //the node data
let key = element.key //the key for the node
let nodeToRemove = FIRDatabase.database().reference().child(key)
nodeToRemove.removeValue()
}
}
})

Jay Jay
Answer

I believe you are going to want to remove child by it's parent key, not the child.

Frank provided a short comment (which is actually THE answer) but here's a verbose example (Firebase 2.x but you'll get the idea):

ref.queryOrdered(byChild: "posts/username").queryEqual(toValue: "someUsername")
     .observeSingleEvent(of: .value, with: { snapshot in

     if ( snapshot?.value is NSNull ) {
           print("not found")
     } else {
           for child in (snapshot?.children)! {
                let element = child as! FDataSnapshot //the node data
                let key = element.key! //the key for the node
                let nodeToRemove = ref.child(byAppendingPath: key)
                nodeToRemove?.removeValue()
     }
  }
})

something to look for is to make sure your paths are correct.

In the above code, 'key' is the parent node name like

-KUaMd3YgJlQvv_P-kdK

If you were to use

let ref = element.ref

it would be the specific path to that node, including the node name but no other data, like this

root_node/posts/-KUaMd3YgJlQvv_P-kdK

so the idea is once you have that reference, use use the reference to remove the node.

So if ref = root_node/posts/-KUaMd3YgJlQvv_P-kdK

then ref.remove(), it will remove that reference and the child data.