0

I have two tables TrendingUsers and Follow. Functionality required is like fetch users from TrendingUsers table and offer to follow, provided fetched user is not from user's follow list. If user is already get followed then skip.

Follow table has columns follower and leader.

PFQuery *followTableQuery = [PFQuery queryWithClassName:@"Follow"];
[followTableQuery whereKey:@"follower" equalTo:[PFUser currentUser] ];
[followTableQuery whereKey:@"leader" equalTo:@"fetchedUserObject" ];
[followTableQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        if (objects.count) {
          //if following objects array will have single object
        }
        else
        {
            //not following to @"fetchedUserObject" user
        }

    }
  }
 ];

This will confirm me that currentUser is following @"fetchedUserObject" user or not. Now I want to integrate this to the TrendingUsers table query to fetch only such users that currentUser is not following.

1 Answer 1

2

You can simply use nested queries, the docs from Parse are usually a good starting point. Here is a sample code, from what I understood from your question, this should do the trick.

//This is our current user
PFUser *user = [PFUser currentUser];

//The first query, querying for all the follow objects from the current user
PFQuery *followingQuery = [PFQuery queryWithClassName:@"Follow"];
[followingQuery whereKey:@"follower" equalTo:user];

//Now we query for the actual trending users, but we do not want the query to return the users (who are in the @"leader" key) that have been found by the first query
PFQuery *trendingQuery = [PFQuery queryWithClassName:@"TrendingUsers"];
[trendingQuery whereKey:@"objectId" notEqualTo:user.objectId]; //don't return the current user
[trendingQuery whereKey:@"objectId" doesNotMatchKey:@"leader" inQuery:followingQuery]; //I'm supposing that @"leader" is containing the objectId of the specific user that is part of the follow object with the current user
[trendingQuery setLimit:1000];
[trendingQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
    //...
}];

I may have not understood your data structure completely, so you may have to exchange one or more keys in the above code, but basically, this is how you would do this.

Sign up to request clarification or add additional context in comments.

1 Comment

perfect..looking for the same !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.