Parse.com检查friendRequest重复iOS

时间:2021-08-24 16:27:03

I would like to avoid duplication with Parse.com and Cloud Code or iOS Code. Here is my class from database : Parse.com检查friendRequest重复iOS

我想避免与Parse.com和Cloud Code或iOS Code重复。这是我的数据库类:

I would like when "from" userId has already sent to "to" userId, that doesn't send the second friendRequest.

我希望当“from”userId已经发送到“to”userId时,它不会发送第二个friendRequest。

Here is my iOS Code :

这是我的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 个解决方案

#1


Hi You can do As Follow to Fetch if you already sent a Friend Request or from"CurrentUser" to "SelectedUser":

嗨如果您已发送好友请求或从“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{

        }

    }];
}

By the PFQuery:

通过PFQuery:

You Search if the values of "CurrentUser" & "SelectedUser" are available in the same row in the Parse.com Serve ! If yes it will return an Array "temp"...if not it will return an array as well...but we gonna count this array if it's 0 (so it means there is no Values inside it, in small words...NO FRIEND REQUEST"

您搜索Parse.com服务的同一行中是否有“CurrentUser”和“SelectedUser”的值!如果是,它将返回一个数组“temp”...如果不是它也将返回一个数组...但是如果它为0则我们将计算这个数组(所以这意味着它里面没有值,用小字说.. .NO FRIEND REQUEST“

if (NSArray*temp.count == 0) save a request !

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

Hope this helps you ! for me it Works

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

#1


Hi You can do As Follow to Fetch if you already sent a Friend Request or from"CurrentUser" to "SelectedUser":

嗨如果您已发送好友请求或从“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{

        }

    }];
}

By the PFQuery:

通过PFQuery:

You Search if the values of "CurrentUser" & "SelectedUser" are available in the same row in the Parse.com Serve ! If yes it will return an Array "temp"...if not it will return an array as well...but we gonna count this array if it's 0 (so it means there is no Values inside it, in small words...NO FRIEND REQUEST"

您搜索Parse.com服务的同一行中是否有“CurrentUser”和“SelectedUser”的值!如果是,它将返回一个数组“temp”...如果不是它也将返回一个数组...但是如果它为0则我们将计算这个数组(所以这意味着它里面没有值,用小字说.. .NO FRIEND REQUEST“

if (NSArray*temp.count == 0) save a request !

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

Hope this helps you ! for me it Works

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