Reputation: 5089
I have an object with a to-many relationship. It goes Workout<-->>Workout Score. If I have the workout score, how can I access the workout? I am using Parse.com.
Assuming workoutScore is a PFObject, and has been retrieved, I have a relationship called whichWorkout on it. The object returned is the workout, however I cannot access it's properties. Am I doing something wrong?
// Assuming this score has been retrieved by a PFQuery
PFObject *workoutScore;
PFObject *actualWorkout = workoutScore[@"whichWorkout"];
// Now when I try to access a property of actualWorkout, I can't
NSString *name = actualWorkout[@"name"];
If I just query for the actual workout, the same code works. Is there any way to access properties of objects retrieved via pointer relationships using Parse?
Upvotes: 0
Views: 90
Reputation: 1569
If you are running a query on the workoutScore you should use [query include:@"workout"]
. This will pull the object that is being pointed to (the actualWorkout) and get everything you need in one call
PFQuery query = [PFQuery queryWithClassName:@"WorkoutScore"];
[query includeKey:@"workout"];
[query findAllInBackground....
The other option is to call fetch on the actual workout. This will be a second call after you have fetched the workoutScore. If you know you are going to need the workout probably better to get it during the query with the include.
PFObject *actualWorkout = workoutScore[@"whichWorkout"];
// There are asynchronous versions of fetch too
// which would be recommended
[actualWorkout fetch];
// actualWorkout will now have its data.
NSString *name = actualWorkout[@"name"];
Upvotes: 1
Reputation: 3591
If the many to many relationship is a PFRelation, you'll have to query it as a second step
PFRelation *relation = [parseObject relationForKey:@"relationName"];
PFQuery *query = [[relation query] findObjectsInBackground:...];
If the many to many relationship is just an array of pointers, you need to tell the query to include the actual data:
[workoutScoreQuery includeKey:@"whichWorkout"];
Upvotes: 1
Reputation: 62676
If WorkoutScore has a back pointer to Workout (it looks like it does from your code, called whichWorkout), then it's easy. When querying WorkoutScore, use includeKey:
to aggressively fetch the related object:
[workoutScoreQuery includeKey:@"whichWorkout"];
Upvotes: 1