1

I have an app where user can vote on a photo, then move on to the next. I need to know the most efficient strategy to keep track of the photos a user has already voted on, so he will not be presented the same photo twice.

My current strategy is to keep in each user profile an array with all already voted photos ids. I then fetch new photos and make sure their ids don't match any id in the array.

I don't think this is the proper strategy as arrays are supposed to contain a limited number of items.... and in this case a user could have viewed A LOT of images.

I don't think neither that it would be appropriate to create a class with an entry for each vote.

There are several options available in Parse.com documentation about "relations" but i'm really not sure the one to choose to:

1) Keep track photos seen by user 2) Fetch new photos excluding those

If you know the best way to do this, please advise. I'm on iOS swift / but this problem is language agnostic.

Robert Brax
  • 6,508
  • 12
  • 40
  • 69
  • You can add all 'voted' photos in core data and make a query in it to find if photo present. – turbo Mar 03 '15 at 21:53
  • Indeed that's an interesting solution. But I prefer if the data downloaded is already filtered. – Robert Brax Mar 03 '15 at 21:55
  • 1
    You can add a PFRelation to attach VotedPhotos(PFFiles) to a user. Then do a inner query where Key DoesNotMatchKey to filter Voted Photos out of your All Photos Query. http://stackoverflow.com/questions/28780592/how-to-do-a-not-in-query-in-parse/28785896#28785896 – ericgu Mar 03 '15 at 21:57
  • so if I understand you correctly, each photo entry, will have PFRelation to all users that voted on it, correct ? – Robert Brax Mar 03 '15 at 22:02

1 Answers1

1

I finally established relations between photos and users as suggested:

 var query = PFQuery(className:"Photos")
    query.getObjectInBackgroundWithId(objectId) {
        (photoEntry: PFObject!, error: NSError!) -> Void in
        if error != nil {
            NSLog("%@", error)
        } else {
            var relation = photoEntry.relationForKey("UsersThatVoted")
            relation.addObject(PFUser.currentUser())
            photoEntry.saveInBackgroundWithBlock {
            //Rest of the query

where "UsersThatVoted" is the relation field in 'Photos' class, that links to Parse default Users class. This is triggered each time user has voted on a photo.

Then, it is very easy to make a regular query to exclude those photos with relation to current user, so he don't see twice a same photo:

query.whereKey("UsersThatVoted", notEqualTo: PFObject(withoutDataWithClassName:"_User", objectId:me.objectId))

Where me is current user. You just add this line to a regular query (to 'Photos' in my case), you don't even need to do a second inner query.

Robert Brax
  • 6,508
  • 12
  • 40
  • 69