在我的项目中,我可以点击其他用户的个人资料。当个人资料打开时,我检查用户是否公开以及他们是否是 friend 。这是通过 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()
}
}
}
如果用户是公开的,并且这两个用户不是 friend ,那么我使用添加 friend 按钮将页面设置为这样。但是,如果用户是私有(private) 并且这两个用户不是 friend ,那么我会使用以下方法关闭该页面:
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"
两个用户都是 friend ,所以当我点击 user2 个人资料时,我可以看到个人资料,一切正常,当 areTheyFriends
运行时,在控制台中显示:
the friends snapshot = Snap (friends) {
BgwmyThLOuhmzwsaCvM0Z6ILDNn1 = 1;
}
这是当前用户好友的快照。但是,当我浏览 user2 个人资料时,我可以单击他们的列表之一,这会打开他们列表的新页面。从这里我可以点击 user2 的名字,这将我再次带到他们的个人资料。像这样:
这次快照看起来有点不同:
the friends snapshot = Snap (friends) {
vreBtOydi2e2DbPxQBdKBhoN1c82 = 1;
}
快照显示了 user2 的 friend ,尽管我从来没有这么叫过。显然,它认为我们不是 friend ,因为快照没有 user2 uid 的 hasChild
,因此调用了 profileIsPrivateAndNotFriends()
方法。
有谁知道出了什么问题以及为什么当个人资料页面不止一次显示时快照发生变化?数据库在此期间永远不会更改。谢谢
最佳答案
在 View 之间切换时如何设置 areTheyFriends() 的“用户”变量?可能是它没有被第二次设置,所以 areTheyFriends 的 bool 闭包将始终返回 false。
关于ios - 多次加载同一屏幕时 firebase 快照发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54311697/