To add to the comments in the david answer above (I can't add a comment yet) to get a count for likes, you want to use transactional data.
In your firebase you want to configure the "likes" of the child, it looks something like this in the node message:
{ "posts": { "post_1": { "uid": "user_1", "title": "Cool Post" "likes": 0 }, "post_2": { "uid": "user_1", "title": "Another Cool Post" "likes": 0 }, "post_3": { "uid": "user_2", "title": "My Cool Post" "likes": 0 }
The code in Xcode looks something like the one below. You will add a counter every time a message is liked (the same code, but use "- 1" to not like it).
self.databaseRef.child("posts").child("post_1").child("likes").runTransactionBlock({ (currentData:FIRMutableData!) in var value = currentData.value as? Int //check to see if the likes node exists, if not give value of 0. if (value == nil) { value = 0 } currentData.value = value! + 1 return FIRTransactionResult.successWithValue(currentData) })
Hope this helps someone!
Additional reading for such a counter:
Upvote / Downvote system in Swift through Firebase
Counter counter not updating node in firebase
gk103
source share