Parse.com检查friendRequest重复iOS

时间:2015-05-13 11:44:19

标签: ios objective-c parse-platform cloud-code friendrequest

我希望通过 Parse.com Cloud Code iOS代码来避免重复。 这是我的数据库类: Screenshot

我想当“来自”userId已经发送到“”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
    }
}];

1 个答案:

答案 0 :(得分:3)

嗨如果您已发送好友请求或来自" 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 Serve的同一行中可用!如果是,它将返回一个数组" temp" ...如果不是它也将返回一个数组......但如果它是0,我们将计算这个数组(所以它意味着没有其中的价值,用小词...... 没有朋友请求"

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

希望这对你有所帮助!对我来说,它的工作原理