Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jack Ryder
7,286 PointsParse Query that retrieves a list of friends and then a list of posts by those friends
So on Parse I have a class for users and a class for Posts. I'm wanting to retrieve the list of friends the current user has and then use that list to query parse for posts which have the same senderId of one of the current User's friends.
So far my code looks like this:
- (void)viewDidLoad
{
[super viewDidLoad];
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
NSLog(@"Current User: %@", currentUser.username);
}
else {
[self performSegueWithIdentifier:@"Segue1" sender:self];
}
if (currentUser){
self.friendsRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"];
PFQuery *query = [self.friendsRelation query];
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"error: %@ %@", error, [error userInfo]);
}
else {
self.friends = objects;
NSLog(@"holla %@", self.friends);
//PFUser *user = self.friends;
PFQuery *queryFriends = [PFQuery queryWithClassName:@"Posts"];
[queryFriends whereKey:@"senderName" containedIn:self.friends];
[queryFriends findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"error: %@ %@", error, [error userInfo]);
}
else {
NSLog(@"hello %@", objects); //not getting these objects
}
}];
}
}];
}
However, I'm not retrieving the objects of the second query.
Any help would be great!
1 Answer
Stone Preston
42,016 Points[queryFriends whereKey:@"senderName" containedIn:self.friends];
self.friends is an array of PFUser objects, not an array of sender names. So thats why its not finding anything. None of the values in self.friends are going to match the value in the senderName field of your post. If you wanted to keep running the query this way, you would need to iterate over your self.friends array and create a mutable array and add each friends userId/username whichever you are using to that array of usernames/ids. then run your current query on that array of usernames/ids instead of the array of friends objects. Then it will find some matches.
In my opinion, it would be easier to create a PFRelation and relate posts to users that way, instead of what you are currently doing. Once you have posts related to users, after you find your friends all you would have to do is iterate through your friends array and query each friends postRelation.