Firebase ios快照为.childRemoved返回null [英] Firebase ios snapshot returning null for .childRemoved

查看:117
本文介绍了Firebase ios快照为.childRemoved返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始与Swift一起使用Firebase,并且有一个示例消息节点。我添加消息持有的文本和用户名等孩子我有安全规则设置为每个.write和.read认证。我创建了一个观察器来监视这个,它正在触发,但是它返回的快照是空的。我需要快照数据的原因是我可以从客户端删除确切的消息。
$ 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)
})
解决方案



您可能想要以另一种方式来解决这个问题:

每个快照(或子快照)都有一个键,这就是您应该在数组中查找的内容表,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆