我目前有这个工作代码,它从 Firebase 中的 users
位置读取,其中包含每个用户的所有用户个人资料信息。我正在使用从 Firebase 返回的快照(数据)来构建我的数组并在 CollectionView
中显示用户。
我预期的行为(没有发生)是当 user
属性的值在 Firebase 中更改时(例如;online
状态更改为“离线” ) 这应该被推送到我的应用程序,以便 CollectionView
更新,因此不会显示刚刚“离线”的用户。
我在下面使用的 Firebase 查询是否正确?
self.REF_USERS.observeSingleEvent(of: .value , with: { (snapshot) in
guard let usersSnapshot = snapshot.children.allObjects as? [DataSnapshot] else { return }
for user in usersSnapshot{
let discoverable = user.childSnapshot(forPath: "discoverable").value as! Bool
if user.key == key && discoverable == true {
let uid = user.key
let name = user.childSnapshot(forPath: "name").value as! String
let email = user.childSnapshot(forPath: "email").value as! String
let profilePictureURL = user.childSnapshot(forPath: "profilePictureURL").value as! String
let birthday = user.childSnapshot(forPath: "birthday").value as! String
let firstName = user.childSnapshot(forPath: "firstName").value as! String
let lastName = user.childSnapshot(forPath: "lastName").value as! String
let gender = user.childSnapshot(forPath: "gender").value as! String
let discoverable = user.childSnapshot(forPath: "discoverable").value as! Bool
let online = user.childSnapshot(forPath: "online").value as! Bool
let discoveryPrefs = user.childSnapshot(forPath: "discoveryPrefs").value as! [String : Any]
let dictionary: [String : Any] = ["uid": uid, "name": name, "email": email, "profilePictureURL": profilePictureURL, "birthday": birthday, "firstName": firstName, "lastName": lastName, "gender": gender, "discoverable": discoverable, "online": online, "discoveryPrefs": discoveryPrefs]
let user = User(uid: uid, dictionary: dictionary)
users.append(user)
}//end if
}//end for
//filter out "remove" current user from array
let filteredUsers = users.filter({ (user: User) -> Bool in return !user.uid.contains(uid) })
handler(filteredUsers.shuffled(), true)//return a shuffled version of the array via handler
})//end FIR snapshot call
这就是我使用上面的代码加载/重新加载数据的方式:
DataService.run.getUsersAtVenue(forVenueLocation: location, forUid: userId) { (users, successBool) in
if successBool {
self.users = users
self.collectionView.reloadData()
Utilities.run.dismissSVHUD(delay: 0.5)
}
}
最佳答案
您是否在主线程中,并且 users
发生了变化?尝试将其放入:
DispatchQueue.main.async {
if users.count > 0 {
self.users = users
self.collectionView.reloadData()
Utilities.run.dismissSVHUD(delay: 0.5)
}
}
关于Swift CollectionView.reload 数据在 Firebase 中发生变化时不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50923308/