Kishon Daniels
Kishon Daniels

Reputation: 55

Correct way to user PFQuery to query relational data like Pointer

Ok so let's say I have a post of an event, and users can click a button to notify that they are attending this event. As of now I have a class called Activity in which I save the current user and the event to this class, so theres 2 columns. If I want to query all users who are attending an event, am I headed in the right direction to do this, or am I doing it complentely wrong?

So far I have:

-(PFQuery*)queryForTable {

PFQuery *activityQuery = [PFQuery queryWithClassName:@"Activity"];

[activityQuery whereKey:@"event" equalTo:self.event];
[activityQuery includeKey:@"going"];

return activityQuery;
}

cellForRowAtIndex:

 UILabel *title = (UILabel*) [cell viewWithTag:1];
 title.text = [object objectForKey:@"going.username"];

Upvotes: 0

Views: 249

Answers (2)

Lucas Huang
Lucas Huang

Reputation: 4016

You can actually see what you have been done in the Parse dashboard. That's also their purpose to develop a data browser like this. It's way more convenient.

For your case, you just need to check whether the type is Pointer. Try to click on that if so in the dashboard. It will direct you to the target object.

Would suggest you to read this article first, it's about the relation: https://parse.com/docs/relations_guide Then, you should go check the iOS SDK tutorial: includeKey is definitely what you need to use.

Here is the sample from Parse:

PFQuery *query = [PFQuery queryWithClassName:@"Comment"];

// Retrieve the most recent ones
[query orderByDescending:@"createdAt"];

// Only retrieve the last ten
query.limit = 10;

// Include the post data with each comment
[query includeKey:@"post"];


[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
    // Comments now contains the last ten comments, and the "post" field
    // has been populated. For example:
    for (PFObject *comment in comments) {
         // This does not require a network access.
         PFObject *post = comment[@"post"];
         NSLog(@"retrieved related post: %@", post);
    }
}];

Upvotes: 1

scott
scott

Reputation: 1194

Your code looks right on so far. Then to retrieve your Activity class values, you can use:

PFQuery *activityQuery = [PFQuery queryWithClassName:@"Activity"];
// Set contraints here, example:
[activityQuery setLimit:100];

[query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
if (!error) {
// Success, do something with your objects.
}
}];

Upvotes: 1

Related Questions