大家好,我一直在使用 firebase 从数据库中写入和读取值。在我编写一个函数来检索以下列方式使用 swift 存储的值或产品之前,它的工作非常棒。
这是我的代码
func retrieveLiveUrlFor(product: Product){
if let path = product.prodRef{
print("Looking for : \(path)")
var liveUrl = ""
let ref = Database.database().reference(withPath: path)
ref.observe(. value, with: {
snapshot in
print("Snap : \(snapshot.value)")
if snapshot.exists(){
print("Snap : \(snapshot.value)")
let dic = snapshot.value as? NSDictionary
if dic != nil{
let url = dic?["liveUrl"] as? String
print("Url is here")
if url != nil{
print("URL is not nil")
liveUrl = url as! String
}
}
}
if (self.productdelegate != nil){
print("Calling Product delegate")
self.productdelegate?.liveUrlObtained!(liveUrl: liveUrl)
}
})
}
}
这是我要检索的路径的值
Products/Live/Global/WpMvDJZUclRlfHFJsSlBEbi0jHf1
这是 firebase 数据库的快照
snapshot.value 总是在 swift 中返回 null。 当我使用
打印时print("Snap: \(snapshot.value)")
打印如下
Snap : Optional() Please guide me what I am doing wrong so that I can get to it.
最佳答案
如果您使用 .value 事件进行观察,则返回 snapshot.value 为 nil 意味着快照值在该引用中不存在。尝试打印整个快照和 snapshot.key
理想情况下,您需要的引用是
let ref = Database.database.reference().child("Products").child("Live").child("Global").child("WpMvDJZUclRlfHFJsSlBEbi0jHf1")
观察者的功能如下:
ref.observe(.value) {(snapshot) in
print(snapshot.value!)
}
关于ios - Firebase 快照返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45600424/