Question

I'm trying to query for objects based on the User who posted the PFObject being in the current user PFRelation "friendship". I was wondering if you can use a PFRelation as you would a NSArray and do something similar to this..

@"user" is a pointer to the PFUser who posted the PFObject.

PFQuery *query = [PFQuery queryWithClassName:TV_TIMELINE_POSTS];
[query includeKey:@"user"];
[query whereKey:@"user" containedIn:[PFUser currentUser][@"friendship"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    [self.objectArray addObjectsFromArray:objects];
}];

Or if the only way to achieve this would to be to fill an NSArray with the PFUsers from the PFRelation, and then query with the results

 PFRelation *relation = [[PFUser currentUser] relationForKey:@"friendship"];
 PFQuery *relationQuery = [relation query];
 [relationQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
     self.followingArray = [[NSMutableArray alloc] initWithArray:objects];
 }];

PFQuery *query = [PFQuery queryWithClassName:TV_TIMELINE_POSTS];
[query includeKey:@"user"];
[query whereKey:@"user" containedIn:self.followingArray];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    [self.objectArray addObjectsFromArray:objects];
    [self.collectionView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    [self.refreshControl performSelectorOnMainThread:@selector(endRefreshing) withObject:nil waitUntilDone:NO];
     _isLoading = NO;
}];

Thanks in advance, Evan

Was it helpful?

Solution

I THINK the first should work. But if not, you can combine the two queries in your last suggestion in one query. So instead of first getting the friendship objects and then fire off a new query, you use matchesQuery in your second query and put in the first.

Your second suggestion alone will not work, as the findObjectsInBackgroundWithBlock will be performed in the background and self.followingArray will not be populated when the second query triggers.

I am unsure if the matchesQuery will work like containedIn. Try it out. If you want to go for your second suggestion, you need to put the second query INSIDE the block of the first query.

PFRelation *relation = [[PFUser currentUser] relationForKey:@"friendship"];
PFQuery *relationQuery = [relation query];

PFQuery *query = [PFQuery queryWithClassName:TV_TIMELINE_POSTS];
[query includeKey:@"user"];
[query whereKey:@"user" matchesQuery:relationQuery];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    [self.objectArray addObjectsFromArray:objects];
    [self.collectionView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    [self.refreshControl performSelectorOnMainThread:@selector(endRefreshing) withObject:nil waitUntilDone:NO];
     _isLoading = NO;
}];

Disclaimer I haven't been able to test this code where I am now, but it should give you the general concept to work from.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top