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

Mutual updated friend list

Hi, I'm trying to make the friend list cross update. For example, if person "2" friends person "3". When 3 logs in, on her friend's tab, 2 should automatically be populated. I'm trying to do this in the didSelectRow method with the EditFriendViewController, but i'm getting exception errors. Appreciate your help:

```-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
PFUser *user = self.allUsers[indexPath.row];
PFRelation *relationCurrent = [self.currentUser relationForKey:@"friendsRelation"];
PFRelation *relationOther=[user relationForKey:@"friendsRelation"];



if ([self isFriend:user]) {
    cell.accessoryType = UITableViewCellAccessoryNone;
    for (PFUser *friend in self.friends) {
        if ([user.objectId isEqualToString:friend.objectId]) {
            [self.friends removeObject:user];
            break;
        }
    }
    [relationCurrent removeObject:user];
    [relationOther removeObject:self.currentUser];

} else {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [self.friends addObject:user];
    [relationCurrent addObject:user];
    [relationOther addObject:self.currentUser];
}

[self.currentUser save];
[user save];

}

3 Answers

Stone Preston
Stone Preston
42,016 Points

there is a work around, however it doesnt look very simple. you have to use cloud code (never used it before so I cant help you there, its javascript by the way)

check out this post on stack overflow: http://stackoverflow.com/questions/18649197/parse-com-saving-attributes-on-a-user-fetched-from-a-query-i-e-not-on-the-cu

here is a post on the parse forum where they mention having to use cloud code: https://parse.com/questions/user-cannot-be-saved-unless-they-have-been-authenticated-via-login-or-signup

Thanks man! This was super helpful.

Stone Preston
Stone Preston
42,016 Points

pretty sure this is your issue here:

} else {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [self.friends addObject:user];
    [relationCurrent addObject:user];
    [relationOther addObject:self.currentUser];
}

[self.currentUser save];
[user save];

in order to add objects to a relation, that object first has to be saved to the backend in order to have an object id. Currently you are adding unsaved objects to a relation then attempting to save. you are probably getting an exception that states something along the lines of "object must have valid object Id before being added to the relation" or something. So you will have to save the user objects, add them to the relation, then save them again.

Thanks for your answer Stone. The exception reason I'm getting is this." *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'User cannot be saved unless they have been authenticated via logIn or signUp' "

Does this mean I just can't access user's relationship status, while currentUser is logged in?

Do you think there's a work around?

-