Parse.com PFUser检索/更新数组类型的属性

时间:2014-08-02 01:52:25

标签: objective-c xcode parse-platform

我正在使用Parse。在特殊的Parse User类(PFUser)中,我为每个用户添加了一个“array”类型的属性。现在,我希望能够检索/更新此数组。该数组称为“friendsList”。

我正在使用Xcode / iOS Parse SDK。

编辑:我尝试了几个不同的解析查询,但它们没有工作。我猜这与你查询特殊类名“User”的事实有关,你不能使用典型的查询,如

[PFQuery queryWithClassName:@"User"]; // This is wrong.

我想要完成的是:currentUser尝试按用户名添加好友。他们输入用户名,按Go键,然后用以下代码搜索特殊的User类:

PFQuery *friendQuery = [PFUser query];
[friendQuery whereKey:@"username" equalTo:userNameToAdd];
[friendQuery getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) 
{ 
    /* This is where I want to now push 'userNameToAdd' onto the friendsList Array inside the currentUser, but I can't figure out how to retrieve/update that Array called friendsList */ 

}

1 个答案:

答案 0 :(得分:0)

这是原始代码,在{callback code block}中有正确的答案。看那里的评论。顺便说一句,这只会添加实际的"用户名" PFUser到friendsList数组。如果您愿意,可以修改代码以将实际的PFUser保存在数组中。

PFQuery *friendQuery = [PFUser query];
[friendQuery whereKey:@"username" equalTo:userNameToAdd];
[friendQuery getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) 
{ 
    /* This is how you add 'userNameToAdd' to the Array called friendsList that
     * belongs to the special parse class called User (PFUser type): */

    // This is your current user
    PFUser *currentUser = [PFUser currentUser];

    // Adds object to friendList array
    [currentUser addObject:userNameToAdd forKey:@"friendsList"];

    // Saves the changes on the Parse server. This is necessary to update the actual Parse server. If you don't "save" then the changes will be lost
    [[PFUser currentUser] saveInBackground]; 

}