0

I'm using Firebase and try to implement Like Button such as Facebook or Instagram.

I have written some code , but I have noticed that the number of likes sometimes increase by more than one like when user taps the like button many times, very fast.

Code...

func handleLike(likeButton: UIButton, numberLabel: UILabel) { guard let uid = FIRAuth.auth()?.currentUser?.uid else { return } if let photoId = photo?.id { let ref = FIRDatabase.database().reference() let photoRef = ref.child("users").child(uid).child("likes").child(photoId) photoRef.observeSingleEventOfType(.Value, withBlock: { (snapshot) in if snapshot.value is NSNull { likeButton.setImage(UIImage(named: "LikeFilled"), forState: .Normal) likeButton.setTitleColor(UIColor.redColor(), forState: .Normal) ref.child("users").child(uid).child("likes").child(photoId).setValue(true) ref.child("photos").child(photoId).child("likes").child(uid).setValue(true) self.photo?.adjustLikes(true) if let numberofLikes = self.photo?.numberofLikes { ref.child("photos").child(photoId).child("numberofLikes").setValue(numberofLikes) numberLabel.text = String(numberofLikes) + "Likes" } } else { likeButton.setImage(UIImage(named: "UNLike"), forState: .Normal) likeButton.setTitleColor(UIColor(r:143, g: 150, b: 163), forState: .Normal) ref.child("users").child(uid).child("likes").child(photoId).removeValue() ref.child("photos").child(photoId).child("likes").child(uid).removeValue() self.photo?.adjustLikes(false) ref.child("photos").child(photoId).child("numberofLikes").setValue(self.photo?.numberofLikes) if let numberofLikes = self.photo?.numberofLikes { ref.child("photos").child(photoId).child("numberofLikes").setValue(numberofLikes) numberLabel.text = String(numberofLikes) + "Likes" } } }, withCancelBlock: nil) } } class Photo: NSObject { func adjustLikes(addLike: Bool) { if addLike { numberofLikes = numberofLikes! + 1 } else { numberofLikes = numberofLikes! - 1 } } } 

How can I implement synchronous function such as LIKE/UNLIKE function ? I thought that I could use with CompletionBlock, but I couldn't implement it with .observeSingleEventOfType...

I appreciate any help...

1
  • Give your JSON tree structure as text not as image Commented Aug 16, 2016 at 16:44

1 Answer 1

1

I have been also trying to implement like counter but using parse as baas. The best solution i have adopted is to don't update the counter value on server directly but set a time out when the time out finish update on server with last state (liked or not):

  1. Check if liked or not and invert the like state : liked = !liked

  2. Update the local Counter Correspondingly : liked ? likeCounter++ : likeCounter--

  3. Update The UI : likeLabel.text = "\(likeCounter)"

Finally check if there is a timer set to update server like state :

if(likeTimer != nil) { // Stop current operation likeTimer.invalidate() } //Setup a set time out 1s func likeTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(updateLikeCounter), userInfo: nil, repeats: false) func updateLikeCounter(){ //if liked == true and the like operation is not submitted to server ==> increment like counter on server . else do nothing //if like == false and a like operation have been submitted to server (user is in the list of users who like the image) ==> decrement like counter on server . else do nothing } 

I hope this can help you

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I will try it. That's the very idea that I did not think of. I really appreciate that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.