Reputation: 2575
I can't find the proper solution to this problem. In the app I am creating, I allow users to change their username whenever they wish. So I do the following when they go to the settings page and update their info:
PFUser *modifiedCurrent = [PFUser currentUser];
[modifiedCurrent setObject:username.text forKey:@"username"];
[modifiedCurrent setObject:name.text forKey:@"additional"];
[modifiedCurrent setObject:email.text forKey:@"email"];
[modifiedCurrent saveInBackgroundWithBlock:^(BOOL success, NSError *error) {
if (error) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Error"
message: [NSString stringWithFormat:@"%@",[[error userInfo]
objectForKey:@"error"]]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
} else {
[self.navigationController popViewControllerAnimated:YES];
};
}];
The server would obviously return an error if the user has chosen a username already in use and thus won't update the database. HOWEVER, the cached version of [PFUser currentUser] seems to update the username field regardless of whether there was an error or not. How do I prevent this from happening? Or how do I revert to the previous "legitimate" username?
Upvotes: 1
Views: 496
Reputation: 6731
I would query to see if the user with that username existed before attempting the change. I'm actually surprised that an error is actually registering on the collision.
Otherwise, you could call fetch on the user object.
Upvotes: 2