Support Topics Documentation Slack YouTube Blog

Adding "Likes" to posts (iOS)

Thanks for all the support i’ve received thus far. Working with backendless has been a very positive experience.

Would anyone be willing to contribute their thoughts on how to best add likes to a post?

Requirements:
A user can only like a post once
A user wants to see a feed of all the posts they have liked.

Thoughts:
Does a post track all the users that has liked it?
Does a user track all the posts it has liked?

Nice to have:
A post can show the names of a user who liked it

From the top of my head, and it is the simplest one:
Create relation One-to-One between User and Post tables.
So if you need to ‘like’ the post, you set relation for the particular user on definite post.
With such approach you will have possibility to make requests with ‘where clause’ for filtering purposes.

A few follow up questions Oleg, thanks for responding!

With a one to one wouldn’t a user be able to not like multiple posts?
Also how would a post keep track of how many users liked it?

FYI the solution I came up with is quite convoluted.

  1. create a like table
  2. When a user likes a post create a new like object (the creator’s id is the likers id and then add the post id as another variable)
  3. When a user loads their feed look up all that users likes (so we can compare the posts with the users likes and tint the like image the right color to indicate they have liked it and also to be able to know to delete the like object if a user unlikes a post)
  4. Also store a “likes” integer on the posts table that gets incremented and decremented as a user likes and delikes a post.

Follow up question: Is there a way to create calculated variables in tables? That are maybe calculated in backend code?

Yes, you are right. I misunderstood you. I’ve thought in the way ‘one user = one post’.
So, just use One-to-Many from User to Post.

Also i would recommend to investigate our documentation about Data service api (how to work with where clause and relations).
You can start from here: https://backendless.com/docs/ios/data_search_with_where_clause.html

I’ve read through all that documentation. Pretty good stuff. There does seem to be a few stop gaps in functionality that could likely be made up through the business logic with time to learn how to use the business logic.

I tried the One-to-Many but when I retrieved the user object relation with the post it returned ALL the user information which just doesn’t work for privacy issues. I couldn’t figure out how to restrict the returned information of the user relation to a subset (IE if the user has email, firstName, lastName and nickname, I only want the relationship to return the variable for nickname for all the user object relations that come down and the other variables should be nil or empty)

Does that make sense? Is there a way to do that with relations? The documentation doesn’t clarify.

When you build query object you are able to point set of properties and set of relations which you need.

For fine security configuration every user may be related to one or a number of roles. And you have a possibility to set access rules for data.

Hi Chris,

if you use relations API, you may set properties field in your query. In this field you should set the names of the properties which values must be retrieved from the server. The argument should be a string in the following format: "propertyName1, propertyName2, , , propertyNameN" , where single property identifies a property/column to be retrieved.