Ben Wheeler
Ben Wheeler

Reputation: 7384

Parse: PFUser signUpInBackgroundWithBlock causing exception when trying to convert anonymous user to regular

I'm using Parse on ios; this is my third app using Parse, but the first one where I've used the anonymous user functions.

I'm trying to convert an anonymous user to a regular user:

[PFAnonymousUtils logInWithBlock:^(PFUser *user, NSError *error) {
    if (error) {
        DLog(@"Anonymous login failed.");
        handler(NO, @"anonymous login failed");
    } else {
        DLog(@"Anonymous user logged in.");
        MyParseUser *myUser = [MyParseUser currentUser];
        if ([PFAnonymousUtils isLinkedWithUser:myUser]) {
            DLog(@"still anonymous");
        } else {
            DLog(@"not anonymous");
        }
        [myUser setParseUserForInstallation]; // usually overkill
        NSString *username = myUser.username;
        NSString *pass = @"password";
        myUser.password = pass;
        DLog(@"Anonymous user has username %@, password %@", username, pass);
        [(PFUser*)myUser signUpInBackgroundWithBlock:^(BOOL success, NSError *error) {
            if (success) {
                DLog(@"signup succeeded");
                handler(YES, myUser);
            } else if (error) {
                DLog(@"Uh oh. An error occurred: %@", error);
                handler(NO, error);
            } else {
                DLog(@"signup didn't throw error, but user not created correctly");
                handler(NO, nil);
            }
        }];
    }
}];

...but signUpInBackgroundWithBlock is causing an exception:

"Cannot sign up an existing user."

Why?

Upvotes: 0

Views: 721

Answers (1)

Ben Wheeler
Ben Wheeler

Reputation: 7384

Turns out there's one and only one username string that is not valid for an anonymous user to have when calling signUpInBackgroundWithBlock: the one it already has! Parse seems to consider the anonymous and non-anonymous users to be separate, so just like two regular users, they can't share the same username. This fixed it:

        NSString *username = [MyParseUser randomStringWithLength:10];
        myUser.username = username;

(details of random string creator not important here)

Upvotes: 2

Related Questions