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

iOS

Getting Relational Data from Parse different class

I have got parse backend set up and recording relational data between two classes. So lets say "Current User" is associated with "John" in the agent class through the agentRelation. All I am trying to do is display some for the strings (name and email) from "john" into a label outlet. The querying the relation I can do but not sure how to output the "name" string. Any help??

.m file

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.agentRelation = [[PFUser currentUser] objectForKey:@"agentRelation"];
    PFQuery *query = [self.agentRelation query];
    NSString *name = [query @"name"];
    self.agentName.text = name;

}

.h

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@interface IBThirdViewController : UIViewController

@property (nonatomic, strong) PFRelation *agentRelation;
@property (nonatomic, strong) NSArray *agent;
@property (weak, nonatomic) IBOutlet UILabel *agentName;

@end

2 Answers

your query logic is incorrect. you need to call findObjectsInBackground with block to actually perform the query and get data

self.agentRelation = [[PFUser currentUser] objectForKey:@"agentRelation"];
PFQuery *query = [self.agentRelation query];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
  if (!error) {
    // success

    // Do something with the found objects
    for (PFObject *object in objects) {
        NSLog(@"name: %@", [object objectForKey:@"name"]);
        NSLog(@"email: %@", [object objectForKey:@"email"]);
    }

  } else {
    // Log details of the failure
    NSLog(@"Error: %@ %@", error, [error userInfo]);
  }
}];

Thank you!!! This makes so much more sense after seeing it. I was tried PFObject as well but know see I first have to query a block of the relational info then do the PFObjects from that. Worked great.

Thank you!!! This makes so much more sense after seeing it. I was tried PFObject as well but know see I first have to query a block of the relational info then do the PFObjects from that. Worked great.

glad I could help : )