解析好友请求代码不起作用



我编写了一些代码,以便一个用户可以向另一个用户发送朋友请求,并且可以接受或拒绝。

当你查看他们的个人资料并按下"发送好友请求"按钮时,这是执行的:

PFQuery *query = [PFUser query];
[query whereKey:@"objectId" equalTo:self.userID];
PFUser *selectedUser = (PFUser *)[query getFirstObject];
PFUser *currentUser = [PFUser currentUser];
//request them
PFObject *friendRequest = [PFObject objectWithClassName:@"_User"];
friendRequest[@"from"] = currentUser;
//selected user is the user at the cell that was selected
friendRequest[@"to"] = selectedUser;
// set the initial status to pending
friendRequest[@"status"] = @"pending";
[friendRequest saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (succeeded) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yay" message:@"Friend request sent" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    else {
        NSLog(@"Error");
    }
}];

但是我得到这个错误:[Error]: Caught "NSInternalInconsistencyException" with reason "User cannot be saved unless they are already signed up. Call signUp first."

问题是两个用户显然已经注册了…我很困惑。欢呼。

您需要通过以下操作注册用户(这也将保存用户):

user.signUpInBackgroundWithBlock { (success, error) -> Void in
            print("signup");
}

你不需要user.saveInBackgroundWithBlock

最新更新