Search code examples
iosobjective-cparse-platformpfquery

Get all objects from one Parse class that are not mentioned in another class


I'm trying to query all records from confessions class whose author is not [PFUser currentUser]... but only those that our [PFUser currentUser] didn't rate on in ratings class.

confessions class:

enter image description here

ratings class: enter image description here

Basically, I want to connect these two queries into one (somehow):

// get all confessions from other users
PFQuery *qConfessions = [PFQuery queryWithClassName:@"confessions"];
[qConfessions whereKey:@"author" notEqualTo:[PFUser currentUser]];

// get all ratings from this user
PFQuery *qRatings = [PFQuery queryWithClassName:@"ratings"];
[qRatings whereKey:@"ratedBy" equalTo:[PFUser currentUser]];

// get all qConfessions that are not in qRatings.confession
// YOUR HELP HERE :)

If there is no easy way to achieve what I want, do you think I should change the model and how? Should I just fetch all the ratings and then somehow ignore all qConfessions that are equal to ratings.confession? Any help would be welcome. Thank you.


Solution

  • I've made a workaround by adding a confessionId field to ratings class on Parse and using the following code:

    // get all ratings from this user
    PFQuery *qRatings = [PFQuery queryWithClassName:@"ratings"];
    [qRatings whereKey:@"ratedBy" equalTo:[PFUser currentUser]];
    
    // get all confessions from other users
    PFQuery *qConfessions = [PFQuery queryWithClassName:@"confessions"];
    [qConfessions whereKey:@"author" notEqualTo:[PFUser currentUser]];
    
    // only fetch confessions that are not rated by current user
    [qConfessions whereKey:@"objectId" doesNotMatchKey:@"confessionId" inQuery:qRatings];
    
    // get all confessions from other users that are not rated by current user 
    [qConfessions findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // Success
        } else {
            // Error
        }
    }];