Search code examples
iosparse-platformmbaas

How to retrieve objects from Parse.com whose key matches a certain value


I want to retrieve an array of users who are "searching" and then randomly select one from the array and add it as a "partner". Here's what I have:

- (IBAction)searchForPlayer:(id)sender {

[self.currentUser setObject:@1 forKey:@"searching"];
[self.currentUser saveInBackground];



PFQuery *findPotential = [PFQuery queryWithClassName:@"User"];
[findPotential whereKey:@"searching" notEqualTo:@0];
[findPotential findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    NSLog(@"Potential players: %i",objects.count);
    if(error){
        NSLog(@"Error!");
    }
    else {
        if (objects.count == 0) {
            NSLog(@"None found");
        }
        else {
            int partnerNum = arc4random_uniform(objects.count)+1;
            PFUser *newPartner = [objects objectAtIndex:partnerNum];
            PFRelation *partners = [self.currentUser relationForKey:@"partners"];
            [partners addObject:newPartner];
        }

    }
}];

[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if(error){
        NSLog(@"Error!");
    }
}];


}

For some reason, no objects are ever found with the specified constraint (whereKey:@"searching" notEqualTo:@0). Does anyone know how to fix this?

Thank you!


Solution

  • The immediate error is in your query declaration. The users class is not represented by the string "Users", it's actually "_Users". However, you should create a Users query with:

    PFQuery *findPotential = [PFUser query];