【问题标题】:iOS - Firebase return null but field with data existsiOS - Firebase 返回 null 但包含数据的字段存在
【发布时间】:2017-02-10 19:39:35
【问题描述】:

我有数据库“通知”。

{
  "Notification": {
      "user_id": 
      "date":
      "type":
      "info": {
           "who_id": 
           "whom_id": 
        }
    }
}

当我第一次启动应用程序时,我会从“通知”中获取并获取所有数据。当我向“通知”添加新数据时,我获取的数据没有“信息”字段。只有字段:日期、类型、user_id。

我的代码:

func fetchNotitfications() {
        FIRDatabase.database().reference().child("Notification").queryOrdered(byChild: "user_id").queryEqual(toValue: FIRAuth.auth()?.currentUser?.uid).observe(.childAdded, with: { (snapshot) in

            if let notificationDict = snapshot.value as? [String:AnyObject] {
                let notification = Notification()
                notification.setValuesForKeys(notificationDict)

                self.notificationTableView.arrayNotification.append(notification)
                self.notificationTableView.reloadData()

            }   
        })
    }  

【问题讨论】:

  • 请将您的 Firebase 结构发布为文本,而不是图像。文本是可搜索的,图像不是,当我们提供一个简洁的答案时,我们需要能够复制和粘贴您的结构,而我们不能用图像来做到这一点。
  • 我想如果你在你的闭包中添加一个快速的print(snapshot),你可能会发现一些有趣的数据。另一个注意事项;请记住,当您使用 .childAdded 时,它将迭代并返回所有符合条件的子节点,并再次为添加的任何符合条件的新节点返回该数据。

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

当您使用 childadded 时,返回的快照与您预期的不同。

而不是得到`

"Notification": {
      "user_id": 
      "date":
      "type":
      "info": {
           "who_id": 
           "whom_id": 
        }
    }

根据查询过滤数据的方式,您很可能会得到以下结果。

["user_id": 
  "date":
  "type":
  "info": {
       "who_id": 
       "whom_id": 
    }
 ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 2019-07-07
    • 2018-09-14
    相关资源
    最近更新 更多