【发布时间】:2018-01-13 14:11:52
【问题描述】:
我已经成功地从 Firebase 为所有用户提取数据,但是在尝试根据 userId 为特定用户提取数据时我遇到了困难。
我已成功将 userId 传递给函数。我已经用 print(userId) 对此进行了测试。
似乎 userdataSnapshot 包含正确的数据,但是“for user in userdataSnapshot”正在循环遍历用户的所有子项。首先是“firstName”,然后是“lastName”.. 而不是将所有这些值都放入数组中.. 感觉错误在于 for 循环。
查看代码后的输出。
// Get all data about a specific user
func getUserdata(userId: String, handler: @escaping (_ userdata: [User]) -> ()) {
print("UserId")
print(userId)
var userArray = [User]()
REF_USERS.child(userId).observeSingleEvent(of: .value) { (userdataSnapshot) in
guard let userdataSnapshot = userdataSnapshot.children.allObjects as? [DataSnapshot] else { return }
print("DataService")
dump(userdataSnapshot)
for user in userdataSnapshot {
let userId = userId // pull content from Firebase
let firstName = user.childSnapshot(forPath: "firstName").value as? String ?? "" // pull content from Firebase
let lastName = user.childSnapshot(forPath: "lastName").value as? String ?? "" // pull content from Firebase
let name = user.childSnapshot(forPath: "name").value as? String ?? "" // pull content from Firebase
let city = user.childSnapshot(forPath: "city").value as? String ?? "" // pull content from Firebase
let country = user.childSnapshot(forPath: "country").value as? String ?? "" // pull content from Firebase
let profileImageURL = user.childSnapshot(forPath: "profileImageURL").value as? String ?? "" // pull content from Firebase
let defaultHunt = user.childSnapshot(forPath: "defaultHunt").value as? String ?? "" // pull content from Firebase
let user = User(userId: userId, firstName: firstName, lastName: lastName, name: name, city: city, country: country, profileImageURL: profileImageURL, defaultHunt: defaultHunt)
userArray.append(user)
}
//print("DataService array content test")
// dump(userArray)
handler(userArray)
}
}
终端输出
UserId
NhZZGwJQCGe2OGaNTwGvpPuQKNA2
DataService
▿ 9 elements
- Snap (city) Oslo #0
- super: NSObject
- Snap (country) Norway #1
- super: NSObject
- Snap (defaultHunt) Hjortejakt #2
- super: NSObject
- Snap (email) christian.simonsen@gmail.com #3
- super: NSObject
- Snap (firstName) Christian #4
- super: NSObject
- Snap (lastName) Simonsen #5
- super: NSObject
- Snap (name) Christian #6
- super: NSObject
- Snap (profileImageURL) https://firebasestorage.googleapis.com/v0/b/shoota-179610.appspot.com/o/gghh%2F985188F8-1439-4906-8BDA-497EAEBDD99A?alt=media&token=359fa0b0-6a90-4393-ab11-a354437f701d #7
- super: NSObject
- Snap (provider) Firebase #8
- super: NSObject
userArray
▿ 9 elements
▿ Shoota.User #0
▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
- some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
- firstName: ""
- lastName: ""
- name: ""
- city: ""
- country: ""
- profileImageURL: ""
- defaultHunt: ""
▿ Shoota.User #1
▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
- some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
- firstName: ""
- lastName: ""
- name: ""
- city: ""
- country: ""
- profileImageURL: ""
- defaultHunt: ""
▿ Shoota.User #2
▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
- some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
- firstName: ""
- lastName: ""
- name: ""
- city: ""
- country: ""
- profileImageURL: ""
- defaultHunt: ""
▿ Shoota.User #3
▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
- some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
- firstName: ""
- lastName: ""
- name: ""
- city: ""
- country: ""
- profileImageURL: ""
- defaultHunt: ""
▿ Shoota.User #4
▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
- some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
- firstName: ""
- lastName: ""
- name: ""
- city: ""
- country: ""
- profileImageURL: ""
- defaultHunt: ""
▿ Shoota.User #5
▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
- some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
- firstName: ""
- lastName: ""
- name: ""
- city: ""
- country: ""
- profileImageURL: ""
- defaultHunt: ""
▿ Shoota.User #6
▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
- some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
- firstName: ""
- lastName: ""
- name: ""
- city: ""
- country: ""
- profileImageURL: ""
- defaultHunt: ""
▿ Shoota.User #7
▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
- some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
- firstName: ""
- lastName: ""
- name: ""
- city: ""
- country: ""
- profileImageURL: ""
- defaultHunt: ""
▿ Shoota.User #8
▿ userId: NhZZGwJQCGe2OGaNTwGvpPuQKNA2
- some: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
- firstName: ""
- lastName: ""
- name: ""
- city: ""
- country: ""
- profileImageURL: ""
- defaultHunt: ""
【问题讨论】: