swiftparse-platformpfrelation

How to use PFRelation for liking?


I want to make a social media app where you can like posts and I heard that you can use PFRelation to do that, but I'm not sure how. I checked Parse.com's docs but it gave me limited information on it, so how would I go about doing this?


Solution

  • It all depends on what you want to make it a relation to. Is it users? Is it a post class itself? Is it statistics class (i.e. Number of views, number of likes, active comments)? If you want to create a like feature, for simplicity, I would forego creating an entire relation if that's the only feature you are worried about. Simply create a new column in the backend and increment it every time the user taps on like, and decrease it when they re-tap.

    Let's say you have a Class called Posts. And in Posts you have these columns:

    | objectId | PostTitle (string) | PostDesc (string) | PostImage (File) | Likes (number) | updatedAt | createdAt | etc etc

    All you would need to do is when a user taps on the like button increment the 'Like' column by 1, and vice versa

    If your worried about relations for other reasons than I understand. But this is a suitable avenue to take for something so simple


    someObject.incrementKey("Likes")
    someObject.saveInBackgroundWithBlock {
    (success: Bool, error: NSError?) -> Void in
      if (success) {
      // The like key has been incremented
      } else {
      // There was a problem, check error.description
      }
    }