I just started working with Firebase with Swift and have a example messages node. I add messages children that hold the text and username, etc. I have security rules set to be authenticated for each .write and .read. I created a observer to watch for this even and it is firing but the snapshot it returns is null. The reason I need the snapshot data is so I can remove that exact message from the client side.
Data Node Structure:
{
"messages" : {
"-KcM3SlAQXfjCz01waXF" : {
"name" : "mgardner1994",
"text" : "test23"
},
"-KcM3UKA_U7n2YwhlFeB" : {
"name" : "mgardner1994",
"text" : "test4"
},
"-KcMB_8Ec74HIQGL9adt" : {
"name" : "mitchell_maler",
"text" : "test5"
},
"-KcOC08kLUO-cEWLeICG" : {
"name" : "mitchell_maler",
"text" : "hello"
},
"-KcOC6ZWT6gyVi6pxGF8" : {
"name" : "mitchell_maler",
"text" : "test"
}
}
}
Rules:
{
"rules": {
"messages": {
".read": "auth != null",
".write": "auth != null"
}
}
}
Swift Code:
_refHandleDel = ref.child("messages").observe(.childRemoved, with: { (snapshot) -> Void in
// TODO: Why is snapshot returning nil
let index = self.messages.index(of: snapshot)
self.messages.remove(at: index!)
self.messagesTable.deleteRows(at: [IndexPath(row: index! , section: 0)], with: .automatic)
})
Edit: Added data structure to text instead of an image.