Search code examples
iosobjective-cparse-platformparse-cloud-code

Parse.com check friendRequest duplication iOS


I would like to avoid duplication with Parse.com and Cloud Code or iOS Code. Here is my class from database : Screenshot

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

Here is my iOS Code :

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
    }
}];

Solution

  • Hi You can do As Follow to Fetch if you already sent a Friend Request or from"CurrentUser" to "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:

    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"

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

    Hope this helps you ! for me it Works