简体   繁体   English

使用Cloud Code Parse.com iOS删除PFUser

[英]Delete PFUser with Cloud Code Parse.com iOS

I succeed to add friend with Cloud Code and Parse.com . 我成功通过Cloud CodeParse.com添加了朋友。

Now I would like to delete friend relation with Cloud Code in didSelectRowAtIndexPath 现在我想在didSelectRowAtIndexPath中删除与Cloud Code的朋友关系

My error is " attempt to insert nil object from objects[0]' " 我的错误是“ attempt to insert nil object from objects[0]'

But I don't know what parameters I need to configure, I found the cloud code for main.js : 但是我不知道我需要配置什么参数,我找到了main.js的云代码:

Parse.Cloud.define("removeFriend", function(request, response) 
{
    // here's how to get the client's user making the request
    var user = request.user;

    // consider checking that user && request.params.friend are valid
    // if not, return response.error("missing user or friend id")

    getUser(request.params.friend).then(function(friend) {
        // your code prematurely called response.success() here, thereby canceling any further steps
        friend.relation("friendsRelation").remove(user);
        // return the promise returned by save() so we can chain the promises
        return friend.save();
    }).then(function(result) {
        // only now that the save is finished, we can claim victory
        response.success(result);
    }, function (error) {
        response.error(result);
    });
});

// EDIT - the OP once referred to a getUser function that we assume to be something like this:
// return a promise to get a user with userId
function getUser(userId) {
    var userQuery = new Parse.Query(Parse.User);
    return userQuery.get(userId);
}

Here is my code EditFriends.m : 这是我的代码EditFriends.m:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        PFQuery *query = [PFUser query];
        [query orderByAscending:@"name"];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (error) {
                NSLog(@"Error: %@ %@", error, [error userInfo]);
            }
            else {
                self.allUsers = objects;
                [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
            }
        }];

        self.currentUser = [PFUser currentUser];
        [self loadFriends];
    }

-(void) loadFriends{
    self.friendsRelation = [[PFUser currentUser] objectForKey:@"friends"];
    PFQuery *query = [self.friendsRelation query];
    [query orderByAscending:@"username"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
     {
         if (error) {
             NSLog(@"Error %@ %@", error, [error userInfo]);
         }
         else {
             self.friends = objects;

             [self.tableView reloadData];
         }
     }];
}


    - (BOOL)isFriend:(PFUser *)user {
        for(PFUser *friend in self.friends) {
            if ([friend.objectId isEqualToString:user.objectId]) {
                return YES;
            }
        }

        return NO;
    }

CellForRowAtIndexPath : CellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    PFUser *user = [self.allUsers objectAtIndex:indexPath.row];

    NSString *name = [[self.allUsers objectAtIndex:indexPath.row] valueForKey:@"username"];
    cell.textLabel.text = name;

    if ([self isFriend:user]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    } else {

        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

didSelectRowAtIndexPath : didSelectRowAtIndexPath:

PFUser *selected = [self.allUsers objectAtIndex:indexPath.row];
    if ([self isFriend:selected]) {
        NSLog(@"déjà amis");
//        PFObject *friendRequest = [self.friendRequests objectAtIndex:indexPath.row];
        [PFCloud callFunctionInBackground:@"removeFriend" withParameters:@{@"friendRequest" : selected.objectId} block:^(id object, NSError *error) {

            if (!error) {
                //add the fromuser to the currentUsers friends

                //save the current user
                [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {

                    if (succeeded) {



                    } else {

                    }

                }];

            }
            else {

            }


        }];

    }
else{
    PFUser *selectedUser = [self.allUsers objectAtIndex:indexPath.row];
    //request them
    PFObject *friendRequest = [PFObject objectWithClassName:@"FriendRequest"];
    friendRequest[@"from"] = self.currentUser;
    friendRequest[@"fromUsername"] = [[PFUser currentUser] objectForKey:@"username"];
    //selected user is the user at the cell that was selected
    friendRequest[@"to"] = selectedUser;
    // set the initial status to pending
    friendRequest[@"status"] = @"pending";
    [friendRequest saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {

        if (succeeded) {


            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yay" message:@"Friend request sent" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];



        } else {

            // error occurred
        }
    }];
}

在此处输入图片说明

The iOS code looks okay, and only needs to be sure it sends the correct user objectId. iOS代码看起来还不错,只需要确保它发送正确的用户objectId即可。

The cloud code is close, but must be improved a little: 云代码已接近,但必须进行一些改进:

Parse.Cloud.define("removeFriend", function(request, response) 
{
    // here's how to get the client's user making the request
    var user = request.user;

    // consider checking that user && request.params.friend are valid
    // if not, return response.error("missing user or friend id")

    getUser(request.params.friendRequest).then(function(friend) {
        // your code prematurely called response.success() here, thereby canceling any further steps
        console.log("relation is:" + JSON.stringify(friend.relation("friends")));
        friend.relation("friends").remove(user);
        // return the promise returned by save() so we can chain the promises
        return friend.save();
    }).then(function(friend) {
        // friendship goes both ways, so remove the friend from user's friends
        user.relation("friends").remove(friend);
        return user.save();
    }).then(function(result) {
        // only now that the save is finished, we can claim victory
        console.log("relation is:" + JSON.stringify(result.relation("friends")));
        response.success(result);
    }, function (error) {
        response.error(error);
    });
});

// EDIT - the OP once referred to a getUser function that we assume to be something like this:
// return a promise to get a user with userId
function getUser(userId) {
    var userQuery = new Parse.Query(Parse.User);
    return userQuery.get(userId);
}

EDIT - to call: 编辑-呼叫:

PFUser *selected = [self.allUsers objectAtIndex:indexPath.row];
if ([self isFriend:selected]) {
    NSLog(@"déjà amis");
    [PFCloud callFunctionInBackground:@"removeFriend" withParameters:@{@"friendRequest" : selected.objectId} block:^(id object, NSError *error) {
    // etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM