我正在使用 Parse 和 Facebook 登录。我的应用程序中的第一个屏幕是带有自定义 Facebook 登录按钮的登录屏幕。单击按钮时,它会执行以下操作:
[PFFacebookUtils logInWithPermissions:@[@"email"] block:^(PFUser *user, NSError *error) {
if (!user) {
[[[UIAlertView alloc] initWithTitle:@"Login error" message:error.description delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
return;
}
if (user.isNew) {
user[@"usernameIsSet"] = @NO;
[user saveEventually:nil];
}
if ([user[@"usernameIsSet"] boolValue] == NO) {
[self performSegueWithIdentifier:@"pushRegister" sender:self];
} else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}];
基本上,我将一个名为 usernameIsSet
的属性附加到 PFUser
,因为我想知道用户是否明确提供了用户名,而不是自动解析的用户名分配。这样,应用程序打开的文本时间, Root View Controller (标签栏 Controller )检查用户是否已有用户名,如果没有,则将注册屏幕显示为模态。这是标签栏 Controller 的 viewDidAppear
:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
BOOL usernameIsSet = [currentUser[@"usernameIsSet"] boolValue];
if (!usernameIsSet) {
RegisterViewController *registerScreen = [[UIStoryboard storyboardWithName:@"iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"RegisterViewController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:registerScreen];
[self presentViewController:navController animated:YES completion:nil];
} else {
}
} else {
[self performSegueWithIdentifier:@"presentLogin" sender:self];
}
}
当出现“注册”屏幕并且用户输入用户名并单击“完成”按钮时,将触发以下方法:
- (IBAction)doneButtonClicked:(id)sender {
PFUser *user = [PFUser currentUser];
user[@"username"] = self.textField.text;
user[@"usernameIsSet"] = @YES;
[user saveEventually:nil];
[self dismissViewControllerAnimated:YES completion:nil];
}
我不明白的是:
第一次安装和运行该应用程序时,我上面的所有代码都按预期工作。显示登录,授予 Facebook 权限,然后出现注册屏幕,只是要求用户分配用户名。用户名已分配,user[@"usernameIsSet"]
设置为 @YES
,模态已关闭,标签栏 Controller 的 viewDidAppear
为已调用,并且 currentUser[@"usernameIsSet"]
仍然是 YES
。
但是,当我从 Xcode 终止运行并再次运行时,选项卡栏 Controller 的 viewDidAppear
被执行并且 currentUser[@"usernameIsSet"]
已经是 NO
。
这里有什么问题?我在 Parse 中查看数据浏览器,usernameIsSet
列包含 true 以及用户分配的用户名。
最佳答案
答案似乎是:
saveEventually
does not write through any caches at this time, including thecurrentUser
, if the app has been restarted. You'll need to regularly callfetch
to keep it up-to-date.
关于ios - PFUser currentUser 没有正确保存/检索自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21361280/