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

时间:2015-05-25 17:25:56

标签: 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];
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您需要使用带有主密钥的云代码。主密钥允许您绕过类级别权限和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的更多信息。

您可能还想将更新当前用户的以下部分移动到此云代码功能中。

答案 1 :(得分:0)

要么不要运行查询以查找所有关注者,要么在可以使用主密钥的云代码中执行此操作。