我试图在使用Firebase的社交媒体应用中处理以下并取消追踪。我有一个名为“Follow”的酒吧按钮项目。点击时,它会检查当前的跟踪状态(在viewDidLoad中检索),并相应地调用跟随/取消关注方法。 user
代表页面的所有者,以及currentUser
想要关注/取消关注的人员。Firebase更新孩子的价值观正在消除子女
意外的行为:当第二次跟踪用户时,可以看到数据库中正确的子节点出现,然后消失。他们不应该消失。我刷新了页面以确保节点实际上被删除了。它在每次应用发布后的第一次尝试中正常运行。
这是我的viewDidLoad(负责检索currentUserIsFollowing)。我怀疑问题就出在这里:
override func viewDidLoad() {
super.viewDidLoad()
let userDogRef = Database.database().reference().child("users").child(user.uid!).child("dogs")
let followingRef = Database.database().reference().child("users").child((Auth.auth().currentUser?.uid)!).child("following")
followingRef.observeSingleEvent(of: .childAdded) { (snapshot) in
if snapshot.value == nil {
print("no following found")
return
}
let value = snapshot.value as? NSDictionary
let followingUserUID = String(describing: value!["uid"]!)
if self.user.uid == followingUserUID {
self.currentUserIsFollowing = true
DispatchQueue.main.async {
self.followBarButtonItem.title = "Unfollow"
}
}
}
}
这里是当跟踪/停止追随按钮被窃听称为动作:
@IBAction func followUserButtonPressed(_ sender: Any) {
if !currentUserIsFollowing {
followUser()
return
}
if currentUserIsFollowing {
unfollowUser()
return
}
}
这里是followUser()
方法:
fileprivate func followUser() {
let followingRef = Database.database().reference().child("users").child((Auth.auth().currentUser?.uid)!).child("following")
let followersRef = Database.database().reference().child("users").child(user.uid!).child("followers")
followingRef.childByAutoId().updateChildValues(["uid": user.uid as Any]) { (error, ref) in
if error != nil {
print(String(describing: error?.localizedDescription))
}
}
followersRef.childByAutoId().updateChildValues(["uid": Auth.auth().currentUser?.uid as Any]) { (error, ref) in
if error != nil {
print(String(describing: error?.localizedDescription))
}
}
}
这里unfollowUser()
方法:
fileprivate func unfollowUser() {
let followingRef = Database.database().reference().child("users").child((Auth.auth().currentUser?.uid)!).child("following")
let followersRef = Database.database().reference().child("users").child(user.uid!).child("followers")
followingRef.observeSingleEvent(of: .childAdded, with: { (snapshot) in
if snapshot.value == nil {
print("no following found")
}
let value = snapshot.value as? NSDictionary
let followingUserUID = String(describing: value!["uid"]!)
if self.user.uid == followingUserUID {
snapshot.ref.removeValue()
}
})
followersRef.observeSingleEvent(of: .childAdded, with: { (snapshot) in
if snapshot.value == nil {
print("no followers found")
}
let value = snapshot.value as? NSDictionary
let followerUserUID = String(describing: value!["uid"]!)
if Auth.auth().currentUser?.uid == followerUserUID {
snapshot.ref.removeValue()
}
})
}
这里是我的JSON树的照片:
我不得不做出一些改变来得到这个工作,但它仍然不是我预想的事情。正如您目前编写的那样,创建引用时,您在字符串插值期间添加了一些感叹号。我还必须将if snapshot.value == nil检查更改为if!snapshot.exists(),因为nil与Any的比较失败。 –
另一件事是JSON树看起来不像预期的那样。它现在变成users/currentUser.uid/following/user.uid /(user.uid:true)。我希望能在没有重复孩子的情况下构建它。即users/currentUser.uid/following /(user.uid:true)。当我尝试这个时,每个新用户都会覆盖最后一个。我希望这是有道理的。我怎样才能得到想要的结果?或者我应该保持原样? –