OGeek|极客世界-中国程序员成长平台

标题: iOS - Firebase 返回 null 但包含数据的字段存在 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:55
标题: iOS - Firebase 返回 null 但包含数据的字段存在

我有数据库“通知”。

{
  "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()

            }   
        })
    }  



Best Answer-推荐答案


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

而不是得到`

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

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

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

关于iOS - Firebase 返回 null 但包含数据的字段存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42167395/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4