简体   繁体   中英

Get contents of Relation in PFObject in Parse

I'm using Parse.com for my backend for an iPhone app.

I have a class called "Product", which has a column called "Season". When I query for set of Products, I want to also be able to output data from their related seasons. I put all the products into a PFobject called "object"

This works fine to get the data from the "Product" class

NSLog(@"The PFObject is %@", object);

This only returns the ID of the relation

NSLog(@"The season object is %@", [object objectForKey:@"Season"]);

How do I get the contents of the related season?

You can tell your query that you want to include related objects using includeKey:

http://parse.com/docs/osx/api/Classes/PFQuery.html#//api/name/includeKey :

Example:

[query includeKey:"season"];

Assuming the class is called "season", you can get a PFObject via its id with the following:

PFQuery *query = [PFQuery queryWithClassName:@"season"];
[query getObjectInBackgroundWithId:object[@"season"] block:^(PFObject *season, NSError *error) {
    // Do something with the returned PFObject in the season variable.
    NSLog(@"%@", season);
}];

More information on querying objects can be found on the iOS developer guide for Parse

According to Parse documentation, you have do fetch the related objects :

By default, when fetching an object, related PFObjects are not fetched. These objects' values cannot be retrieved until they have been fetched like so:

PFObject *post = fetchedComment[@"parent"];
[post fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
  NSString *title = post[@"title"];
}];

You can find more information on the relations here : https://parse.com/docs/ios_guide#objects-pointers/iOS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM