Firebase ios快照为.childRemoved返回null [英] Firebase ios snapshot returning null for .childRemoved
问题描述
$ b $ p
数据节点结构:
{
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:{
messages :{
.read:auth!= null,
.write:auth!= null
}
}
}
Swift Code:
观察(.childRemoved,与:{(快照) - >>>无效// TODO:为什么快照返回零
let index = self.messages.index(of:snapshot)
self.messages.remove(at:index!)
self.messagesTable.deleteRows(at:[IndexPath(row:index!,section:0)],with:.automatic)
})
$ p $编辑:添加数据结构到文本而不是图片。
您可能想要以另一种方式来解决这个问题:
每个快照(或子快照)都有一个键,这就是您应该在数组中查找的内容表,dataSource等)
换句话说,当消息最初被加载(例如数据源)时,它们将在
消息
-KcM3SlAQXfjCz01waXF(键)
名称:mgardner1994,(字典的值)
text:test23
理想情况下,您将获得这些键值的数组对
$ $ p $ code $ a $ 0 $ key $ value = -KcM3SlAQXfjCz01waXF:子字典
a [1] = key: value = -KcM3UKA_U7n2YwhlFeB:子字典
注意上面是字典,但可能是UserClass或ItemsClass
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ / code>
当您的应用程序收到一个remove事件时,它将作为快照传递key:value对。我们不关心价值,但我们关心的关键,你可以找到该数组中的项目的索引,并将其删除。 (通过ObjC NSPredicate找到匹配)或者...像这样的事情在Swift中,如果你使用的是一个UserClass的例子
func handleChildRemoved(_ snapshot:FDataSnapshot!){
let key = snapshot.key
如果let index = self.usersArray.indexOf({$ 0.firebaseKey == key} ){
self.usersArray.removeAtIndex(index)
self.usersTableView.reloadData()
}
}
在这个例子中,我有UserClass
$ p $
class UserClass {
firebaseKey =
userName =
}
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.
You may want to go about it in another way:
Each snapshot (or the child snapshots) will have a key and that's what you should be looking up in your array (table, dataSource etc)
In other words, when the messages are initially loaded (into a dataSource for example) they will be in a key:value pair of
messages
-KcM3SlAQXfjCz01waXF (the key)
name : "mgardner1994", (the values which are a dictionary)
text : "test23"
Ideally you will end up with an array of those key value pairs
a[0] = key:value = -KcM3SlAQXfjCz01waXF: dictionary of children
a[1] = key:value = -KcM3UKA_U7n2YwhlFeB: dictionary of children
Note the above is dictionary but it could be a UserClass or ItemsClass
a[0] = aUser (UserClass)
a[1] = aUser (UserClass)
When your app receives a remove event, it will be passed the key:value pair as the snapshot. We don't care about the value but we do care about the key as you can then find the index of that item in the array and remove it. (via ObjC NSPredicate to find the match) or... something like this in Swift if you are using a UserClass for example
func handleChildRemoved(_ snapshot: FDataSnapshot! ) {
let key = snapshot.key
if let index = self.usersArray.indexOf({$0.firebaseKey == key}) {
self.usersArray.removeAtIndex(index)
self.usersTableView.reloadData()
}
}
In this example, I have UserClass
class UserClass {
firebaseKey = ""
userName = ""
}
这篇关于Firebase ios快照为.childRemoved返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!