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 trialGerman Espitia
4,578 PointsCreating a PFRelation and adding objects to it.
Pretty straight forward, my code is below but I keep getting an error for
[relation addObject:object];
.
PFUser * currentUser = [PFUser currentUser];
PFRelation *relation = [currentUser relationForKey:@"habits"];
PFObject *object = [PFObject objectWithClassName:@"Habit"];
object[@"name"] = @"gym";
[relation addObject:object];
[currentUser saveInBackground];
1 Answer
Stone Preston
42,016 Pointsobjects must be saved first before being added to a relation
PFUser * currentUser = [PFUser currentUser];
PFRelation *relation = [currentUser relationForKey:@"habits"];
PFObject *object = [PFObject objectWithClassName:@"Habit"];
object[@"name"] = @"gym";
[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[relation addObject:object];
[currentUser saveInBackground];
}
}];
German Espitia
4,578 PointsGerman Espitia
4,578 PointsThe docs over at Parse.com didn't help much. Not once under the relational data section do they mention saving an object.
but once again, thanks Stone. I appreciate your help.