所以我有一个跟随按钮,第一次点击它时效果很好。以下计数加1,按钮标题更改为"取消关注"。当我点击按钮时,标题成功更改回"关注"但是下面的计数减少了2而不是1.这是我的问题。所以这个方法被调用了两次。我一直试图弄清楚如何解决这个问题,但我似乎无法弄明白。非常感谢帮助。提前谢谢!
class guestProfileViewController: UIViewController, UINavigationControllerDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var followButton: CustomizableButton!
var ref: DatabaseReference?
var otherUser: userAccount!
let loggedInUser = Auth.auth().currentUser
var loggedInUserData: userAccount!
override func viewDidLoad() {
super.viewDidLoad()
loadButtonView()
}
func loadButtonView(){
Database.database().reference().child("following").child(self.loggedInUser!.uid).child(self.otherUser.uid!).observe(.value, with:
{ (snapshot) in
if(snapshot.exists())
{
self.followButton.setTitle("Unfollow", for: .normal)
self.followButton.backgroundColor = .white
self.followButton.setTitleColor(UIColor.rgb(red: 17, green: 154, blue: 237)
, for: .normal)
print("You are following the user")
}else{
self.followButton.setTitle("Follow", for: .normal)
self.followButton.setTitle("Follow", for: .normal)
self.followButton.backgroundColor = UIColor.rgb(red: 17, green: 154, blue: 237)
self.followButton.setTitleColor(.white, for: .normal)
self.followButton.layer.borderColor = UIColor(white: 0, alpha: 0.2).cgColor
print("you are not following user")
}
})
{ (error) in
print(error.localizedDescription)
}
}
@IBAction func followButton(_ sender: CustomizableButton) {
let ref = Database.database().reference().child("user").child(loggedInUser!.uid)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
let dictionary = snapshot.value as? NSDictionary
let loggedInFollowingCount = dictionary?["followingCount"] as! Int
let followersRef = "followers/\(self.otherUser.uid! as String)/\(self.loggedInUser!.uid)"
let followingRef = "following/" + (self.loggedInUser!.uid) + "/" + (self.otherUser.uid! as String)
if(self.followButton.titleLabel?.text == "Follow")
{
let ref = Database.database().reference().child("user").child(self.loggedInUser!.uid)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
let dictionary = snapshot.value as? [String: Any]
let followersData = ["fullName": dictionary?["fullName"] as? String, "profilePic": dictionary?["profilePic"]as? String,"followersCount": dictionary?["followersCount"] as? String,"countryFlagImage": dictionary?["countryFlagImage"] as? String, "userID": dictionary?["uid"] as? String]
let followingData = ["fullName":self.otherUser.fullName! as String, "profilePic": self.otherUser.profilePic! as String, "followersCount": self.otherUser.followersCount as Int, "countryFlagImage": self.otherUser.countryFlagImage!, "userID": self.otherUser.uid!] as [String : Any]
let childUpdates = [followersRef:followersData,
followingRef:followingData]
Database.database().reference().updateChildValues(childUpdates)
print("data updated")
})
Database.database().reference().child("user").child(self.loggedInUser!.uid).child("followingCount").setValue(loggedInFollowingCount + 1)
Database.database().reference().child("user").child((self.otherUser?.uid)!).child("followersCount").setValue((self.otherUser!.followersCount) as Int + 1)
}
else
{
Database.database().reference().child("user").child(self.loggedInUser!.uid).child("followingCount").setValue(loggedInFollowingCount - 1)
Database.database().reference().child("user").child((self.otherUser?.uid)!).child("followersCount").setValue((self.otherUser!.followersCount) as Int - 1)
let followersRef = "followers/\(self.otherUser.uid! as String)/\(self.loggedInUser!.uid)"
let followingRef = "following/" + (self.loggedInUser?.uid)! + "/" + (self.otherUser?.uid)!
let childUpdates = [followingRef:NSNull(),followersRef:NSNull()]
Database.database().reference().updateChildValues(childUpdates)
}
})
}