Parse.com 检查 friendRequest 重复项 iOS

Parse.com check friendRequest duplication iOS

我想避免 Parse.comCloud Code 的重复iOS 代码。 这是我的 class 来自数据库:

我想当“from”userId 已经发送到“to”userId 时,不发送第二个 friendRequest .

这是我的 iOS 代码:

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:@"Sent !" message:@"Friend request sent" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];



    } else {

        // error occurred
    }
}];

您好,如果您已经发送好友请求或从"CurrentUser"发送至"SelectedUser":

,您可以按照以下步骤获取
-(void)fetchfriendrequestAndSave{

    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";

    PFQuery*query = [PFQuery queryWithClassName:@"FriendRequest"];
    [query whereKey:@"from" equalTo:self.currentuser];
    [query whereKey:@"to" equalTo: selectedUser];
    [query findObjectsInBackgroundWithBlock:^(NSArray*FriendRequestArray, NSError*error){

        if(!error){

            NSArray*temp = [NSArray arrayWithArray:object];
            if(temp.count==0){

                //Save & Send Request

                [friendRequest saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {


                    if (succeeded) {


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



                    } else {

                        // error occurred
                    }
                }];


            }else{

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"FriendREquest ERROR" message:@"Friend Request is Already Submitted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];


            }


        }else{

        }

    }];
}

通过 PFQuery:

您搜索 "CurrentUser" 和 "SelectedUser" 的值是否在 Parse.com 服务的同一行中可用!如果是,它将 return 一个数组 "temp"...如果不是,它也会 return 一个数组...但是如果它是 0,我们将计算这个数组(所以这意味着有里面没有值,简而言之...没有好友请求"

if (NSArray*temp.count == 0) 保存请求!

希望对您有所帮助!对我来说它有效