Swift ios Firebase不返回任何数据
我正在尝试使用此函数实时获取值:Swift ios Firebase不返回任何数据,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,我正在尝试使用此函数实时获取值: handle = ref?.child("Users").child(String(itemId)).observe(.childChanged, with: { (snapShot) in if let dictionary = snapShot.value as? [String: Any] { print(dictionary)
handle = ref?.child("Users").child(String(itemId)).observe(.childChanged, with: { (snapShot) in
if let dictionary = snapShot.value as? [String: Any] {
print(dictionary)
if let profileImageUrl = dictionary["url"] as? String {
print(profileImageUrl)
}
}
}, withCancel: nil)
我启动我的应用程序,然后转到我的firebase控制台,对那个孩子进行一些更改,但我的print()
从未被解雇
我的数据库是这样的:
MyRootDb123
-Users
--Id (Child of users)
--- url inside Id
--- name inside Id
--- age inside Id
同样在我的代码中,带取消功能的有什么作用
数据库结构:
更新
我添加了print(snapShot)
,它返回:
Snap (url) www.someurl.com
Firebase对节点密钥名称区分大小写,因此用户与用户不同,并且您的代码正在访问用户,但结构是用户
代码
结构
MyRootDb123
-Users
--Id (Child of users)
users
uid_0
name: "Leonard"
url: "www.leonard.com
uid_1
name: "Bill"
url: "www.bill.com"
更新以显示如何将.childChanged观察者附加到用户节点并处理对用户的更改
让我们从基本的Firebase结构开始
users
uid_0
name: "Leonard"
url: "www.leonard.com
uid_1
name: "Bill"
url: "www.bill.com"
然后我们将.childChanged观察者附加到用户节点。当用户节点中的子节点更改时,更改的节点将传递给闭包
let usersRef = self.ref.child("users")
usersRef.observe(.childChanged, with: { snapshot in
let userDict = snapshot.value as! [String: AnyObject]
let name = userDict["name"] as! String
let url = userDict["url"] as! String
print("\(name) now has url of: \(url)")
})
为了测试这一点,在Firebase控制台中,我们将uid_0的url子级从www.leonard.com更改为www.yipee.com,如下所示
users
uid_0
name: "Leonard"
url: "www.yipee.com" //here's the new url
uid_1
name: "Bill"
url: "www.bill.com"
由于更改发生在uid_0中,因此该节点在上面的代码中传递给闭包,并将打印:
Leonard now has url of: www.yipee.com
哪一个没有被解雇print(dictionary)
?@NazmulHasan没有人被解雇,如果让dictionary=snapShot.value as,这永远不会进入?[String:Any]{
我想我不能使用[String:Any]@NazmulHasan我添加的print(snapShot)
返回:Snap(url)www.someurl.com--Id(用户的孩子)我不知道这意味着什么共享firebase密钥name@NazmulHasan我已经添加了一个dbI的屏幕截图,我在帖子中输入了一个错误。我的代码似乎可以工作,因为当我调用print(snapShot)
时,它会输出“Snap(url)www.someurl.com”但是我不明白如何检查我的节点的特定值console@Kiow屏幕截图显示用户,您的代码显示用户。它区分大小写,不会处理该错误。打印(快照)时它将打印从Firebase检索到的数据-因为快照是空白的,所以它不会打印任何内容。代码正在运行,但不会工作,因为它不会因为拼写错误从Firebase获得任何内容。修复您的帖子和代码中的拼写错误,我们将再次检查这个问题。我现在已经修复了我的拼写错误,但我现在仍然不知道我该怎么做可以查看更新了什么值以及更新到了什么。现在我正在尝试这样做:snapshot.key==“url”{get value}
@Kiow查看我关于如何执行此任务的答案。