iOS-Parse如何检索给定用户名的对象?

时间:2015-07-20 21:20:18

标签: ios objective-c parse-platform

我有两个ViewControllers,在第一个中,每个PFObject中有3个键值对,保存它们以在单击按钮后解析。在第二个ViewController中,我想创建另一个属性并将其保存到同一个PFObject中。这是我的代码:

第一个ViewController中的

- (void)next
{
    PFObject *thisuser = [PFObject objectWithClassName:@"User"];
    thisuser[@"name"] = [PFUser currentUser].username;
    thisuser[@"institution"] = institution.text;
    thisuser[@"major"] = major.text;
    [thisuser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
    {
        if (succeeded)
        {
            GuideViewController2 *GVC2 = [[GuideViewController2  alloc]initWithNibName:@"GuideViewController2" bundle:nil];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:GVC2];
            nav.modalPresentationStyle = UIModalPresentationFullScreen;
            nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            [self presentViewController:nav animated:YES completion:nil];

            NSLog(@"success");
        }
        else
        {
            NSLog(@"nope");
        }
    }];
}

在我的第二个视图控制器中,用户可以上传个人资料照片,我希望这张照片保存在同一个PFObject中。那么有没有办法使用[PFUser currentUser].username属性检索对象?如何在User类下获取此用户对象以添加照片键值对?

THX。

1 个答案:

答案 0 :(得分:0)

您只需查询用户:

PFQuery *query = [PFUser query];
[query whereKey:USERNAME_KEY equalTo:username];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error)
{ 
    if (!error) {
       //You found the user!
       PFUser *queriedUser = (PFUser *)object;
    }

}];

假设用户名实际上是唯一的! USERNAME_KEY将是您的用户名字段,username是实际的用户名。