我想避免与 Parse.com 和 Cloud Code 或 iOS 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
}
}];
嗨,如果您已经发送了好友请求或从“当前用户”到“选定用户”,您可以按照以下方式获取:
-(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) 保存请求!
希望这对您有帮助!对我来说它有效