TooManyEduardos
TooManyEduardos

Reputation: 4404

PFQuery querying n number of PFUser and PFObjects

I'm using Parse.com and I have a question regarding their relational database. I have a User class (the default _User class) that as one of its columns it has a "friends" array. This friends array consists of pointers to other Users.

What I want to do now is populate a facebook-style timeline where at the beginning of the app I query my current user for its array of friends and its posts, but it also queries the user's array of friends and their posts.

I already have the friends array. I can also query my current user, and I can query a specific user.

My question is, how do I efficiently query for posts on n number of users's posts? While I can query specific users 1 by 1, this gets really slow if the user has 100+ friends since I would need to query each of its friends for posts.

Thank you. PS: I'm using iOS.

Upvotes: 0

Views: 163

Answers (1)

knshn
knshn

Reputation: 3461

I assume you have a Post class where you keep the users' posts. Then you can get posts of a number of users by using containedIn

PFQuery *query = [PFQuery queryWithClassName:@"Post"];
[query whereKey:@"user" containedIn:friends]; //friends is an array of users

Or you can go with matchesKey. Some code from AnyPic example:

PFQuery *followingActivitiesQuery = [PFQuery queryWithClassName:kPAPActivityClassKey];
[followingActivitiesQuery whereKey:kPAPActivityTypeKey equalTo:kPAPActivityTypeFollow];
[followingActivitiesQuery whereKey:kPAPActivityFromUserKey equalTo:[PFUser currentUser]];
followingActivitiesQuery.limit = 1000;

PFQuery *photosFromFollowedUsersQuery = [PFQuery queryWithClassName:self.className];
[photosFromFollowedUsersQuery whereKey:kPAPPhotoUserKey matchesKey:kPAPActivityToUserKey inQuery:followingActivitiesQuery];
[photosFromFollowedUsersQuery whereKeyExists:kPAPPhotoPictureKey];

Upvotes: 1

Related Questions