【发布时间】:2019-01-22 15:39:15
【问题描述】:
在我的项目中,我可以点击其他用户的个人资料。当个人资料打开时,我会检查用户是否是公开的以及他们是否是朋友。这是通过 User 类中的这些方法完成的
func areTheyFriends(user: User, _ completion: @escaping (Bool) -> Void){
Database.database().reference().child("users").child(uid).child("friends").observe(.value) { (snapshot) in
print("the friends snapshot = \(snapshot)")
if snapshot.hasChild(user.uid) {
completion(true)
}
else{
completion(false)
}
}
}
func checkIfPublic(_ completion: @escaping (Bool) -> Void){
Database.database().reference().child("users").child(uid).child("publicProfile").observe(.value) { (snapshot) in
let profile = snapshot.value as? Bool
completion(profile!)
}
}
第一种方法将观察 currentUsers 的好友,并查看用户是否在该快照中。第二种方法会检查用户是否是公开的。
这些方法在一个名为 FriendsProfileTableViewController 的类中调用,如下所示:
func checkIfPublic(_ completion: @escaping (Bool) -> Void) {
self.user.checkIfPublic { (isPublic) in
if isPublic == true {
print("the user is public")
completion(isPublic)
}else if isPublic == false{
print("the user is private")
completion(isPublic)
}
}
}
func checkIfFriends(_ completion: @escaping (Bool) -> Void) {
currentUser.areTheyFriends(user: user) { (areFriends) in
if areFriends == false {
print("users are not friends")
completion(areFriends)
}else if areFriends == true{
print("the users are friends")
completion(areFriends)
}
}
}
我给他们打电话viewWillAppear
checkIfPublic { (isPublic) in
self.checkIfFriends { (areFriends) in
if (isPublic == true) && (areFriends == false) {
//is the user is public but are not friends
self.setPageIfNotFriends()
}
if (isPublic == false) && (areFriends == false) {
//if the user is private and they are not friends
self.profileIsPrivateAndNotFriends()
}
if (areFriends == true) {
self.setPageIfFriends()
}
}
}
如果用户是 public 并且两个用户不是朋友,那么我使用添加朋友按钮将页面设置为这样。但是,如果用户是 private 并且两个用户不是朋友,那么我会使用以下方法关闭页面:
func profileIsPrivateAndNotFriends(){
self.popup.showUnsuccessfullAlert(message: "You cannot view this profile. You and \(self.user.firstName) are not friends")
self.navigationController?.popViewController(animated: true)
}
我遇到的问题是当前用户
users --
vreBtOydi2e2DbPxQBdKBhoN1c82 --
birthday: "09/06/1996"
firstName: "Beth"
friends --
BgwmyThLOuhmzwsaCvM0Z6ILDNn1: true
lastName: "jones"
publicProfile: true
uid: "vreBtOydi2e2DbPxQBdKBhoN1c82"
和用户 2:
users --
BgwmyThLOuhmzwsaCvM0Z6ILDNn1 --
birthday: "14/03/1995"
firstName: "Andrew"
friends --
vreBtOydi2e2DbPxQBdKBhoN1c82: true
lastName: "Harris"
publicProfile: false
uid: "BgwmyThLOuhmzwsaCvM0Z6ILDNn1"
两个用户都是朋友,所以当我点击 user2 个人资料时,我可以看到个人资料,一切都很好,当areTheyFriends 运行时,在控制台中会显示:
the friends snapshot = Snap (friends) {
BgwmyThLOuhmzwsaCvM0Z6ILDNn1 = 1;
}
这是当前用户朋友的快照。但是,当我浏览 user2 个人资料时,我可以单击他们的其中一个列表,这会打开他们列表的新页面。从这里我可以单击 user2 名称,再次将我带到他们的个人资料。像这样:
这一次虽然快照看起来有点不同:
the friends snapshot = Snap (friends) {
vreBtOydi2e2DbPxQBdKBhoN1c82 = 1;
}
快照显示的是 user2 的朋友,尽管我从来没有这么称呼过。很明显,它认为我们不是朋友,因为快照没有 hasChild 的 user2 uid,因此调用了 profileIsPrivateAndNotFriends() 方法。
有谁知道出了什么问题以及为什么当个人资料页面多次显示时快照会更改?在此期间,数据库永远不会更改。谢谢
【问题讨论】:
-
您已经很好地解释了这个问题,但是缺少一些代码。例如,当 Andrew Harris 点击他的名字以“返回”包含他的列表的主 Andrew Harris 视图时,执行的代码在哪里。哪些代码填充该列表以及哪些代码用于设置用户属性。我的猜测是你在某个时候覆盖了用户,所以初始用户被点击的用户替换。请记住,重要的是要包含 Minimal, Complete, and Verifiable example,而不是用文字墙打击我们。
标签: ios swift firebase firebase-realtime-database