2

我想避免与Parse.comCloud CodeiOS Code重复。这是我的数据库课程: 截屏

我想当“ 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
    }
}];
4

1 回答 1

3

嗨,如果您已经发送了好友请求或从“CurrentUser”发送到“SelectedUser”,您可以执行 As Follow 来获取:

-(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:

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

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

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

于 2015-05-13T12:21:37.640 回答