将唯一对象添加到用户的PFquery

时间:2015-10-09 17:33:36

标签: ios parse-platform pfuser

我正在尝试将关注用户的人员添加到标题为"关注者"在用户下,从发件人标签的objectatindex中检索。 这是代码:

-(void)followButton:(id)sender {


UIButton *senderButton = (UIButton *)sender;
NSLog(@"current Row=%ld",(long)senderButton.tag);

PFObject *object = [self.objects objectAtIndex:senderButton.tag];

if (![[object objectForKey:@"followers"]containsObject:[PFUser currentUser].objectId]) {

    [[PFUser currentUser] addUniqueObject:object.objectId forKey:@"following"];
    [[PFUser currentUser] saveInBackground];


    PFUser *otherUser = [self.objects objectAtIndex:senderButton.tag];
    NSLog(@"Followed %@", otherUser);

    [otherUser addUniqueObject:[PFUser currentUser].objectId forKey:@"followers"];
    [otherUser saveInBackground];


  // NSLog(@"Followed %@", object);



} else {

    [[PFUser currentUser] removeObject:object.objectId forKey:@"following"];
    [[PFUser currentUser] saveInBackground];

    PFUser *otherUser = [self.objects objectAtIndex:senderButton.tag];
    [otherUser removeObject:[PFUser currentUser].objectId forKey:@"followers"];
    [otherUser saveInBackground];


        }

[self.tableView reloadData];

}

出于某种原因,上面的代码只会添加到"以下"当前用户重新单击时,将删除该对象但没有任何反应。另外,代码

 PFUser *otherUser = [self.objects objectAtIndex:senderButton.tag];
    [otherUser removeObject:[PFUser currentUser].objectId forKey:@"followers"];
    [otherUser saveInBackground];

完全没有任何意义,同时它应该做的事情是添加到"追随者"所选行的用户数组。我究竟做错了什么?获得工作跟随/追随者行动的目标。我正在使用PFQueryTableViewController!

1 个答案:

答案 0 :(得分:0)

您无法从客户端修改其他用户的对象。您只能修改当前用户。修改当前用户的用户的方法是使用Parse.Cloud.useMasterKey();

在云代码中使用主密钥。

https://parse.com/questions/how-can-i-allow-a-user-to-edit-another-user

因此,通过传递“otherUser”的用户名和要删除的objectId,在云代码中创建一个函数

Parse.Cloud.define("RemoveFollower", function(request, response){
var username = request.params.username;
var id_to_remove = request.params.objectId_passed;
var query = new Parse.Query(Parse.User);
query.equalTo("username", username);
query.find({
    success: function(results){
        var u = results[0];
        //u modify the user u however you like...
        u.save();
        response.success("Success");
    },
    error: function(){
        response.error("Error");
    }
    });