Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store likes using Firebase

I have a backend on firebase and there are something like post like in Facebook. So I need functionality liking these posts. The question is how to store likes and user who liked the post? All help will be appreciated

like image 386
Zhanserik Kenes Avatar asked Dec 21 '15 07:12

Zhanserik Kenes


2 Answers

Take this data structure:

{
   "posts": {
      "post_1": {
         "uid": "user_1",
         "title": "Cool Post"
      },
      "post_2": {
         "uid": "user_1",
         "title": "Another Cool Post"
      },
      "post_3": {
         "uid": "user_2",
         "title": "My Cool Post"
      }
   },
   "postLikes": {
      "user_1": {
         "post_3": true
      },
      "user_2": {
         "post_1": true,
         "post_2": true         
      }
   }
}

The location /posts retrieves all the posts. The location /postLikes retrieves all the likes on posts.

So let's say you're user_1. To get the posts user_1 has liked you could write this Firebase database listener:

let ref = Firebase(url: "<my-firebase-app>")
let uid = "user_1"
let userRef = ref.childByAppendingPath(uid)
userRef.observeEventType(.Value) { (snap: FDataSnapshot!) in
  print(snap.value) // prints all of the likes

  // loop through each like
  for child in snap.children {
    let childSnap = child as! FDataSnapshot
    print(childSnap.value) // print a single like
  }
}

What's important to note here is the "flatness" of the data structure. The postLikes are not stored under each post. This means that you can retrieve a post without getting all of its likes. But, if you want to get both, you can still do that since you know the user's id.

Try giving the Firebase guide on Structuring Data a read through

like image 161
David East Avatar answered Oct 22 '22 03:10

David East


To add to the comments in david's answer, above (I can't add a comment yet) to get the count for likes, you want to use transactional data.

In your firebase, you want to set up a "likes" child, looks something like this in the post node:

{
   "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 similar to below. You would be adding a counter every time the post is liked (same code, but use "- 1" to unlike).

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 this sort of counter:

Upvote/Downvote system within Swift via Firebase

Follower counter not updating node in firebase

like image 26
gk103 Avatar answered Oct 22 '22 03:10

gk103