这是我的情况;我需要一个应用程序中所有用户的字典,才能在didSelectRowAt
上使用UITableView
来获取搜索到的用户uid
。
我的firebase
数据库的设计如下:
不允许上传图片,请在此处链接
此外,我通过执行此操作成功创建了string/dictionary
个用户名;
let rootRef = Database.database().reference()
let query = rootRef.child("users").queryOrdered(byChild: "users")
query.observe(.value) { (snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot] {
if let value = child.value as? NSDictionary {
// declaring a user in class User() which was made with strings(name, email, ect.)
let user = User()
let name = value["name"] as? String ?? "name not found"
// making the string of it go into the user.name
user.name = name
// appending it into a variable of class user()
self.users.append(user)
}
}
}
我不确定该怎么做,因为uid's
都不同,并且没有名称(ex. email: test@test.com).
也许我需要重组数据库以允许这样做,但是我不太确定。感谢您的帮助!
答案 0 :(得分:0)
一种方法是保留uid字段以及名称,电子邮件和profileImageURI字段。
let rootRef = Database.database().reference()
guard let uid = rootRef.child("users").childByAutoId().key
现在,将uid作为字段发送,同时将数据保存到firebase。 检索数据时,您可以按以下方式映射数据:
let query = rootRef.child("users").queryOrdered(byChild: "users")
query.observe(.value) { (snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot] {
if let value = child.value as? NSDictionary {
// declaring a user in class User() which was made with strings(name, email, etc.)
var dict = [String:String]()
let user = User()
let name = value["name"] as? String ?? "name not found"
// making the string of it go into the user.name
user.name = name
user.uid = value["uid"]
dict[name] = "\(uid)"
}
}