在我的 Firebase 实时数据库中,我有一个标记为“组”的节点,这就是我构建它的方式:
在上面的“用户”下面,我试图使用这些用户 ID 来引用每个用户中的数据。这就是我构建我试图引用的每个“用户”节点的方式:
在下面的代码片段中,我从组的用户子节点的快照中获取 userId。然后我在这些 userId 上运行 for-in 循环以访问“用户”节点中的信息。
print("This should be the individual userId: ", userId)
语句正确打印出每个 userId。 userRef.observeSingleEvent(of: .value, with: { (snapshot) in
在第一次调用 for-in 循环时被调用,但它几乎就像被忽略了一样。应用程序崩溃是因为user array comes up empty at the end. 然而,数组中出现了大量空用户(当查看调试区域中的 Variables View 时)。所以,我觉得我正在运行某种形式的冗余循环什么的。
guard let groupChatPartnerId = message.chatPartnerId() else {
return
}
var users: [User]?
let ref = Database.database().reference().child("groups").child(groupChatPartnerId)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
let groupId = snapshot.key
let groupName = snapshot.childSnapshot(forPath: "groupName").value as! String
let userIdDictionary = snapshot.childSnapshot(forPath: "users").value as! Dictionary<String,AnyObject>
let userIds = Array(userIdDictionary.keys)
print("userIds: ", userIds)
for userId in userIds {
print("This should be the individual userId: ", userId)
let userRef = Database.database().reference().child("users").child(userId)
userRef.observeSingleEvent(of: .value, with: { (snapshot) in
print("This is the snapshot: ", snapshot)
let email: String = snapshot.childSnapshot(forPath: "email").value as! String
print("user's email: ", email)
let uid = snapshot.key
let username = snapshot.childSnapshot(forPath: "username").value as! String
let profileImageUrl = snapshot.childSnapshot(forPath: "profileImageUrl").value as! String
let user = User(uid: uid, userUsername: username, userProfileImageUrl: profileImageUrl, userEmail: email)
users?.append(user)
print("user to append to users: ", user)
}, withCancel: nil)
}
print("users :", users)
let group = Group(groupId: groupId, groupName: groupName, users: users!)
self.showChatControllerForGroup(group: group)
}, withCancel: nil)
如果您需要任何其他信息,请告诉我。提前致谢!
最佳答案
所有数据都是从 Firebase 异步加载的 当您打印 users
时,userRef.observeSingleEvent
还没有完成。因此,打印所有用户的代码必须位于 userRef.observeSingleEvent
的完成句柄内,并且必须仅在加载所有用户后运行。
一个简单的方法是比较 users
的长度和 userIds
的长度。如果它们相同,则您已加载所有用户:
for userId in userIds {
print("This should be the individual userId: ", userId)
let userRef = Database.database().reference().child("users").child(userId)
userRef.observeSingleEvent(of: .value, with: { (snapshot) in
print("This is the snapshot: ", snapshot)
let email: String = snapshot.childSnapshot(forPath: "email").value as! String
print("user's email: ", email)
let uid = snapshot.key
let username = snapshot.childSnapshot(forPath: "username").value as! String
let profileImageUrl = snapshot.childSnapshot(forPath: "profileImageUrl").value as! String
let user = User(uid: uid, userUsername: username, userProfileImageUrl: profileImageUrl, userEmail: email)
users?.append(user)
print("user to append to users: ", user)
if userIds.count == users.count {
print("users :", users)
}
}, withCancel: nil)
}
关于ios - 为数组中的每个字符串获取 firebase 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57275533/