0

So I've set up my relations correctly in Parse using the following code:

var user = PFUser.currentUser() var relation = user.relationForKey("likes") relation.addObject(self.post) user.saveInBackgroundWithBlock({ (Bool, error) -> Void in println("user likes this post") // Used for tracking in the PFTableCell for UI parts self.userLikes = true })

When the view loads, I need to change the buttons to Liked and the colour etc. I'd like to get all the users who liked this particular post but cannot figure out the query? It will give me some other useful info (e.g. I could display usernames of people who liked the post).

As a workaround I can get all the posts the user likes by doing:

var user = PFUser.currentUser()
var relation = user.relationForKey("likes")
relation.query().findObjectsInBackgroundWithBlock {
    (objects: [AnyObject]!, error: NSError!) -> Void in
    if error != nil {
        // There was an error
        println(error)
    } else {
        // objects has all the Posts the current user liked and do something.
        println(objects)         
    }
}

But that's not what I'm after? I'm using Swift and the Parse SDK, but will happily have the answer in Obj C and I'll translate. The Parse docs suggestion is wrong: https://www.parse.com/docs/ios_guide#objects-pointers/iOS I could store the like against the post rather than the user, but that would mean I'd have to open up access rights to the post which is a no-no

jscs
  • 63,694
  • 13
  • 151
  • 195
ExoticChimp
  • 1,884
  • 1
  • 19
  • 37

1 Answers1

0

There is an issue with your database structure. You should not just have User objects, but you should have Post objects as well. The Post can have a likes relation that points to all the Users who liked the post. When a User likes the Post, then the User is added to the Post's likes relation. Thus, to get the info you need, you just need to look at the Post's relation.

What do you mean about open access rights to the Post? Do you want the post to be read only for all users except the User who posted it?

Or is the issue with giving access to the User objects? I am not sure if it really an issue, since you could just be running a quick count on the User objects in the relation without actually looking at the data in the User objects. You could create Like objects, but I don't think it's necessary.

Josh Gafni
  • 2,831
  • 2
  • 19
  • 32
  • 1
    My database structure is fine, I have Post objects and User objects. The problem is that when I do a relation.query() on the relation object.relationForKey("likes"), it returns null (presumably because Posts don't like users). I've followed the parse.com docs but one of their tutorials suggests setting the likes relationship on the Post (i.e. Post likedBy User). However to do that, I believe all Posts would need to have public read write access, whereas I only want them to have Public read (except for the user who created it of course) – ExoticChimp Mar 19 '15 at 01:26
  • essentially, i'm setting successfully saving the relationship USER likes POST already. Now what i want is to be able to retrieve the inverse i.e. all USERS who like that POST – ExoticChimp Mar 19 '15 at 01:27
  • 1
    I get the issue. In order to update the likes relation, you need write access on the post. Unfortunately, Parse doesn't let you set read/write access by field. I don't really think it matters giving the access to the Post, but if you think so, you could create two different types of Post objects - Post and PostDetail. Post has a postDetail pointer. Post is read only except for the poster, but PostDetail is writeable by everyone. PostDetail contains the likes relation. – Josh Gafni Mar 19 '15 at 01:31
  • 1
    @JoshGafni is right. You have it backwards. You said "My database structure is fine, I have Post objects and User objects. The problem is that when I do a relation.query() on the relation object.relationForKey("likes"), it returns null (presumably because Posts don't like users)." For every post, you need to create a PFRelation and add the current user. Then you query the PFRelation of Posts for all users who liked that post. That's it. – ericgu Mar 19 '15 at 04:35
  • cheers. I was following the Parse.com docs which suggested putting the relation on the user but I'll probably use your suggestion. why the downvote? It's a common question with no clear answer except here (see parse forums) – ExoticChimp Mar 19 '15 at 10:07
  • If you're adding a relation to the `Post` object, wouldn't this require granting public write permission to Posts? – adamdport Nov 11 '15 at 19:33