ios Firebase 在 Swift 中检索数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37759614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Firebase Retrieving Data in Swift
提问by Tim
I'm trying to retrieve specific data from just the currently logged in user. My data in my database looks like this:
我正在尝试仅从当前登录的用户中检索特定数据。我的数据库中的数据如下所示:
For example, I want to just grab the full_name and save it in a variable userName. Below is what I'm using to grab my data
例如,我只想获取 full_name 并将其保存在变量 userName 中。以下是我用来获取数据的内容
ref.queryOrderedByChild("full_name").queryEqualToValue("userIdentifier").observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
print(snapshot.value)
// let userName = snapshot.value["full_name"] as! String
})
Unfortunately, this is what my console prints.
不幸的是,这是我的控制台打印的内容。
I would appreciate any help :) Thank you!
我将不胜感激:) 谢谢!
回答by DogCoffee
It gives you that warning message indexOn
because you are doing a query.
它向您提供警告消息,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
您应该通过安全和 Firebase 规则中的 .indexOn 规则定义您将索引的键。虽然您可以在客户端上临时创建这些查询,但使用 .indexOn 时,您会看到性能大大提高
As you know the name you are looking for you can directly go to that node, without a query.
正如您知道要查找的名称一样,您可以直接转到该节点,无需查询。
let ref:FIRDatabaseReference! // your ref ie. root.child("users").child("[email protected]")
// 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
})
回答by Khawar Islam
Swift 4
斯威夫特 4
let ref = Database.database().reference(withPath: "user")
ref.observeSingleEvent(of: .value, with: { snapshot in
if !snapshot.exists() { return }
print(snapshot) // Its print all values including Snap (User)
print(snapshot.value!)
let username = snapshot.childSnapshot(forPath: "full_name").value
print(username!)
})
回答by Ranjan
let ref = Database.database().reference().child("users/[email protected]")
ref.observeSingleEvent(of: .value, with: { (snap : DataSnapshot) in
print("\(String(describing: snap.value))")
}) { (err: Error) in
print("\(err.localizedDescription)")
}
回答by Roshni
{
"rules": {
"tbl_name": {
".indexOn": ["field_name1", "field_name2"]
},
".read": true,
".write": true
}
}
You can apply indexOn on any field. Add this json in rules security and rules tab. Hope this works for you. :)
您可以在任何字段上应用 indexOn。在规则安全和规则选项卡中添加此 json。希望这对你有用。:)
回答by urvashi bhagat
It retrieves logged in user data:
它检索登录的用户数据:
let ref = FIRDatabase.database().reference(fromURL: "DATABASE_URl")
let userID = FIRAuth.auth()?.currentUser?.uid
let usersRef = ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
print(snapshot)