我手动将字段添加到我的" _用户"关于Parse的表。具体来说,Twitter Handle是一个字符串并且具有键" TwitterHandle。"出于某种原因,当我执行以下语句(并且Twitter句柄为非null)时,我收到null:
NSLog(@"%@", [[PFUser currentUser] objectForKey:@"TwitterHandle"]);
相反,我必须执行以下代码,查询后端以查找当前用户的字段,以及他们的twitter句柄,这看起来多余且过于复杂:
PFUser *currentUser = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query whereKey:@"username" equalTo:currentUser.username];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// do some stuff here with the current user's row in the User table, the only object in
// the NSArray of *objects
NSLog(@"%@", [[PFUser currentUser] objectForKey:@"TwitterHandle"]); --> (non-null)
}];
是否因为我手动设置字段的方式?否则,我不确定为什么我的代码会出现这种行为。
答案 0 :(得分:0)
currentUser
对象在登录时填充并缓存,如果数据正在更改,则需要获取最新副本:
[[PFUser currentUser] fetchInBackground];
答案 1 :(得分:0)
Timothy提到[[PFUser currentUser] fetchInBackground];
将以牺牲Parse的请求为代价获得新专栏。
然而,在开发时,从应用程序注销并重新登录。这将清除缓存并检索所有用户信息。