0
votes

I have a custom Parse class...

Follow
-----------------
Follower - PFUser
Followee - PFUser

I've also added a custom field fullName to the User table.

This can't change. (It can be added to though)

I've done several queries using it and I'm stumbling on this last one.

I'd like to be able to run a query that returns Users whose fullName contains some given text but only users who are following the current user.

i.e.

If Paul and Peter are following me there will be Follow objects where I am the followee and they are the follower. Also, Phillip is not following me so there is no Follow record for him.

If I run this query with the search text @"P" then it should return Peter and Paul and not Phillip.

I just can't work out how to create the query.

I have tried something like this...

PFQuery *followQuery = [CCFollow query];
[followQuery whereKey:@"followee" equalTo:[PFUser currentUser]];

PFQuery *nameQuery = [PFUser query];
[nameQuery whereKey:@"fullName" contains:searchText];
[nameQuery whereKey:@"objectId" equalsKey:@"follower" inQuery:followQuery];

But it returns no error and no objects.

2

2 Answers

1
votes
[nameQuery whereKey:@"objectId" equalsKey:@"follower" inQuery:followQuery];

This line of code is not correct. Here, the key objectId is of type string and followers is of type PFUser. You could create an extra column "followerString" which stores the objectId of the follower in string format so that you can compare.

1
votes

The better option is to switch your query around:

Get all Follow records where followee is the current user and follower is contained in a query of User records that match your fullName query.

PFQuery *followQuery = [CCFollow query];
[followQuery whereKey:@"followee" equalTo:[PFUser currentUser]];

PFQuery *nameQuery = [PFUser query];
[nameQuery whereKey:@"fullName" contains:searchText];

// here's the difference:
[followQuery whereKey:@"follower" matchesQuery:nameQuery];
// include follower
[followQuery includeKey:@"follower"];
// now run find on followQuery (not nameQuery)
[followQuery findObjectsInBackgroundWithBlock:^(NSArray *follows, NSError *error) {
    // "follows" now contains the follow records, and the "follower" field
    // has been populated. For example:
    for (PFObject *follow in follows) {
         // This does not require a network access.
         PFObject *follower = follow[@"follower"];
         NSLog(@"retrieved related user: %@", follower);
         // could put them in an array or whatever to bind to the UI
    }
}];

Doing it this way you don't need to change you schema or start storing objectId references as a string.