ios - 如何解析Firebase实时数据库

标签 ios swift firebase firebase-realtime-database

如何解析 Firebase 实时数据库?

到目前为止我的代码是:

var ref: DatabaseReference!
ref = Database.database().reference()
ref.child("data").observe(.childAdded) { (snapshot) in
   print("snapshot = \(snapshot)")
}

我无法输入条件。 enter image description here

print("url = \(ref.url)")
url = "https://gdenamaz.firebaseio.com"

这个变体也不起作用

var ref: DatabaseReference!
    ref = Database.database().reference().child("data")

    ref.observeSingleEvent(of: .value) { (snapshot) in
        for data in snapshot.children {
            print("data = \(data)")
        }
    }

最佳答案

引用官方文档 -

refHandle = postRef.observe(DataEventType.value, with: { (snapshot) in
  let postDict = snapshot.value as? [String : AnyObject] ?? [:]
  // ...
})

您想要查找 snapshot.value 而不是 snapshot.children

另一个例子

let userID = Auth.auth().currentUser?.uid
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
  // Get user value
  let value = snapshot.value as? NSDictionary
  let username = value?["username"] as? String ?? ""
  let user = User(username: username)

  // ...
  }) { (error) in
    print(error.localizedDescription)
}

此外,.childAdded 仅在添加子项时才会触发。 IE,除非您实际更改该节点引用中的某些内容,否则什么都不会发生。

关于ios - 如何解析Firebase实时数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47713087/

相关文章:

firebase - 一段时间后删除 Firebase 匿名用户

firebase - "Lateinit was never initialized"实际上在 ListenerForSingleValueEvent 中初始化

java - Firestore 根据条件执行删除

iphone - 可以使用 RTSP(实时流协议(protocol))将实时视频流从 iPhone 发送到媒体服务器吗?

ios - 从 Parse 检索 PFFile 时出现问题

iphone - 复制 iPhone 的照片应用程序

ios - 获取自定义单元格的标识符

ios - 一个 Storyboard,两个 Controller

ios - 如何使用 Sprite Kit Physics 创建一个 "cross"(或 "plus")形状的旋转对象?

ios - 如何在 Swift 中使用 UISlider 更改 UIImageView 中的图像