user3398659
user3398659

Reputation: 31

Retrieving all user relations from Parse

I have an app where users add other users and it is saved as a relation on Parse. Here is the code I use to save it/add:

    PFUser *user = (PFUser *)object;
    [friendsRelation addObject:user];
    [self.friends addObject:user];
    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@" %@ %@", error, [error userInfo]);
        }
    }];

Now, in another view controller, I don't really know how I would do to display all of the friends of a user within a TableView.

Upvotes: 0

Views: 500

Answers (1)

Jacob
Jacob

Reputation: 2338

I would look into using PFQueryTableViewController.This allows you to query the relation and display the results in a table. The code for that would look something like this:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Custom the table
        NSLog(@"Init");
        // The className to query on
        self.parseClassName = @"_User";

        // The key of the PFObject to display in the label of the default cell style
        self.textKey = @"firstName";

        // Whether the built-in pull-to-refresh is enabled
        self.pullToRefreshEnabled = NO;

        // Whether the built-in pagination is enabled
        self.paginationEnabled = YES;

        // The number of objects to show per page
        self.objectsPerPage = 25;
    }
    return self;
}

- (PFQuery *)queryForTable {


    PFQuery *query = [self.relation query];




    // If no objects are loaded in memory, we look to the cache first to fill the table
    // and then subsequently do a query against the network.
    if ([self.objects count] == 0) {

        NSLog(@"NONE");
        query.cachePolicy = kPFCachePolicyIgnoreCache;

    }

    [query orderByAscending:@"lastName"];

    return query;
} 

Where self.relationis the relation that you grab from the current user.

EDITL If you do not wish to use PFQTVC (it's the easier option, and contains more features), you can just use a regular tableview. First, get the relation from your current user:

PFRelation *relation = [[PFUser currentUser] relationForKey:@"friends"]; Now, query this relation:

PFQuery *query = [relation query];
NSArray *array = [query findObjects];

Now you can fill your regular table with the contents of that array, which contains PFUser objects.

Upvotes: 1

Related Questions