gpt4 book ai didi

ios - Firebase 在 Swift 中检索数据

转载 作者:搜寻专家 更新时间:2023-10-30 22:02:16 24 4
gpt4 key购买 nike

我正在尝试从当前登录的用户那里检索特定数据。我的数据库中的数据如下所示:

enter image description here

例如,我只想获取全名并将其保存在变量 userName 中。以下是我用来获取数据的内容

ref.queryOrderedByChild("full_name").queryEqualToValue("userIdentifier").observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
print(snapshot.value)
// let userName = snapshot.value["full_name"] as! String
})

不幸的是,这是我的控制台打印的内容。

enter image description here

如果有任何帮助,我将不胜感激 :) 谢谢!

最佳答案

它会向您显示警告消息 indexOn,因为您正在执行查询。

you should define the keys you will be indexing on via the .indexOn rule in your Security and Firebase Rules. While you are allowed to create these queries ad-hoc on the client, you will see greatly improved performance when using .indexOn

因为您知道要查找的名称,您可以直接转到该节点,而无需查询。

    let ref:FIRDatabaseReference! // your ref ie. root.child("users").child("stephenwarren001@yahoo.com")

// only need to fetch once so use single event

ref.observeSingleEventOfType(.Value, withBlock: { snapshot in

if !snapshot.exists() { return }

//print(snapshot)

if let userName = snapshot.value["full_name"] as? String {
print(userName)
}
if let email = snapshot.value["email"] as? String {
print(email)
}

// can also use
// snapshot.childSnapshotForPath("full_name").value as! String
})

关于ios - Firebase 在 Swift 中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37759614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com