Zhanserik Kenes
Zhanserik Kenes

Reputation: 335

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

Upvotes: 9

Views: 6425

Answers (3)

rhalf
rhalf

Reputation: 21

i think the best way to store likes is to create an array variable...then push their uid... if they unlike just pull out their uid... the size of the array will be the numbers of likes. this way you will have no duplicates.

 "post_1": {
     "uid": "user_1",
     "title": "Cool Post"
     "likes": []
  }

post_1.likes.push(user.uid)

Upvotes: 1

gk103
gk103

Reputation: 377

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

Upvotes: 8

David East
David East

Reputation: 32604

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

Upvotes: 14

Related Questions