How to ensure that the value of a thing does not becomes negative?

Okay, so in my app I let users like each other’s post. But in some cases due to difference in time the number of likes gets negative. How can I ensure that this does not happen?

@fayewatson

Hard to answer without seeing the editor. How did you set up the “Likes” counter?

I would suggest using logic that’s a bit stronger than If liked then increase post’s liked count by 1 / if unliked then decrease post’s liked count by 1. (A like should preferably be tied to a user, and the likes (in a simple model) would be a list of users who liked the post. So, if they click “Like,” then the relevant user is added to the List of Users who liked the post, similar logic for “Unlike”

2 Likes

I think this should work