【发布时间】:2018-06-19 07:55:50
【问题描述】:
我目前有这个工作代码,它从 Firebase 中的 users 位置读取,其中包含每个用户的所有用户个人资料信息。我正在使用从 Firebase 返回的快照(数据)来构建我的数组并在 CollectionView 中显示用户。
我预期的行为(没有发生)是,当 Firebase 中 user 属性的值发生更改时(例如,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)
}
}
【问题讨论】:
-
当用户离线时你从
online得到什么?True/False? -
我目前正在将
discoverable的值更改为true或false,此字段指定是否应在collectionview 上显示用户。online也一样,true或false