ios - 将数据保存到另一个用户对象

标签 ios objective-c parse-platform

我正在尝试将当前用户的用户名保存到与另一个用户关联的数组中,但 ACL 不允许将数据写入另一个用户对象。该代码允许您输入用户名,如果该用户存在,它将将该用户名添加到您关注的一组用户中。当您开始关注某个用户时,您的用户名需要添加到您刚关注的人的关注者数组中。我该怎么做?

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        UITextField *alertTextField = [alertView textFieldAtIndex:0];
        self.username = alertTextField.text;

        PFQuery *query = [PFUser query];
        [query whereKey:@"username" equalTo:self.username];
        PFUser *user = (PFUser *)[query getFirstObject];

        NSLog(@"User: %@", user[@"username"]);

        if (!(user[@"username"] == NULL)) {
            if (![self.followersList containsObject:user[@"username"]]) {
                [self.followersList addObject:user[@"username"]];
                [[PFUser currentUser]setObject:self.followersList forKey:@"Following"];
                [[PFUser currentUser]saveInBackground];

                NSMutableArray *otherUsersFollowers;

                if ([user objectForKey:@"Followers"] == NULL) {
                    otherUsersFollowers = [[NSMutableArray alloc]init];
                }
                else {
                    otherUsersFollowers = [user objectForKey:@"Followers"];
                }

                [otherUsersFollowers addObject:[PFUser currentUser].username];
                [user setObject:otherUsersFollowers forKey:@"Followers"];
                [user saveInBackground];

                [self.tableView reloadData];
            }
            else {
                UIAlertView *alreadyFollow = [[UIAlertView alloc]initWithTitle:@"" message:@"You already follow that user." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alreadyFollow show];
            }
        }
        else {
            UIAlertView *noUserAlert = [[UIAlertView alloc]initWithTitle:@"" message:@"That user does not exist" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [noUserAlert show];
        }
    }
}

最佳答案

您需要使用带主 key 的云代码。主 key 允许您绕过类级权限和 ACL。在您的 iOS 代码中调用您的云函数:

NSDictionary *params = @{@"otherUserId": @(user.objectId), @"username": [PFUser currentUser].username};
[PFCloud callFunctionInBackground:@"addFollower" withParameters:params block:^(id object, NSError *error) {
    // Probably you want to reload your table here    
}];

云函数可以是这样的:

function addFollower (request, response) {
    var user = request.user;
    var otherUserId = request.params.otherUserId;
    var username = request.params.username;
    if (!user) {
        response.error("Need to login");
        return;
    } else if (!otherUserId) {
        response.error("Need the other user's id");
        return;
    } else if (!username) {
        response.error("Need the current user's username");
        return;
    }

    var otherUser = Parse.User.createWithoutData(otherUserId);
    otherUser.addUnique("followers", username);
    otherUser.save(null, {useMasterKey:true}).then(function (otherUser) {
        response.success();
    }, function (error) {
        response.error(error);
    })
}

Parse.Cloud.define("addFollower", addFollower);

请注意,我将 saveuseMasterKey:true 选项一起使用,以便在该特定保存中绕过 ACL。相反,您还可以在此函数的开头添加一行 Parse.Cloud.useMasterKey();,这样您就可以在函数内的所有操作中绕过 ACL。更多信息在 the docs .

您可能还想将更新当前用户关注的部分移到此云代码函数中。

关于ios - 将数据保存到另一个用户对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30443250/

相关文章:

objective-c - 选中抑制按钮时禁用 NSAlert 按钮

ios - 在 instantiateViewControllerWithIdentifier 之后实例变量不是 nil?

ios - 警告 : Function definition inside an Objective-C object is deprecated

ios - viewWillAppear ext 的 JASidePanels 委托(delegate)未被调用

ios - 只允许 2 个用户从公共(public)类读取一个对象

javascript - 如何创建注销脚本 - Parse (JavaScript)

swift - 在 Swift 中从解析中检索图像

ios - NodesAtPoint 未找到节点

ios - Away3d 4 + IOS StageWebView + 视频全屏 = View3D 空

iphone - 在 NSMutableArray.. 中添加对象是否有任何限制?