解析:PFUser signUpInBackgroundWithBlock在尝试将匿名用户转换为常规用户时导致异常

时间:2014-07-03 18:44:28

标签: ios xcode parse-platform

我在ios上使用Parse;这是我使用Parse的第三个应用程序,但是我使用匿名用户函数的第一个应用程序。

我正在尝试将匿名用户转换为普通用户:

[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);
            }
        }];
    }
}];

...但是signUpInBackgroundWithBlock导致异常:

"Cannot sign up an existing user."

为什么呢?

1 个答案:

答案 0 :(得分:2)

原来只有一个用户名字符串对匿名用户在调用signUpInBackgroundWithBlock时无效:它已经拥有的那个! Parse似乎认为匿名和非匿名用户是分开的,因此就像两个普通用户一样,他们不能共享相同的用户名。这修好了它:

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

(随机字符串创建者的详细信息在这里不重要)