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 Build a Self-Destructing Message iPhone App Relating Users in Parse.com Removing Friends by Tapping on a Table View Cell

Creating 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
Stone Preston
42,016 Points

objects 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];

        }
    }];

The 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.