我正在尝试查询“朋友”关系,同时排除任何已阻止用户的朋友(存储为关系< _user>)与朋友用户关系到他们阻止的用户(可能是用户检查他们的朋友)。我试过像下面那样查询,但它并没有像我想的那样排除记录:
[SVProgressHUD showWithStatus:@"Loading ..."];
[friends removeAllObjects];
PFRelation *friendRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"];
if (friendRelation)
{
PFQuery *friendsQuery = [friendRelation query];
[friendsQuery orderByAscending:@"firstname"];
[friendsQuery whereKey:@"disabled" notEqualTo:[NSNumber numberWithBool:YES]];
[friendsQuery whereKey:@"Blocked" notEqualTo:[PFUser currentUser]];
[friendsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error)
{
friends = [objects mutableCopy];
[self.myTableView reloadData];
[SVProgressHUD dismiss];
}
else
{
[SVProgressHUD showErrorWithStatus:@"Loading failed. Please try again."];
}
}];
}
else
{
[SVProgressHUD dismiss];
}
有没有办法完成我想要做的事情?
答案 0 :(得分:0)
我在其中一个项目中遇到了同样的问题并按如下方式解决了问题。我创建了一个课程"阻止"一旦用户阻止另一个用户,我只需在那里创建两个对象。我在一般辅助类中做了这个,通过创建方法(user1
是当前用户,user2
是另一个用户):
void BlockUserOne(PFUser *user1, PFUser *user2)
{
PFQuery *query = [PFQuery queryWithClassName:PF_BLOCKED_CLASS_NAME];
[query whereKey:PF_BLOCKED_USER equalTo:[PFUser currentUser]];
[query whereKey:PF_BLOCKED_USER1 equalTo:user1];
[query whereKey:PF_BLOCKED_USER2 equalTo:user2];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (error == nil)
{
if ([objects count] == 0)
{
PFObject *object = [PFObject objectWithClassName:PF_BLOCKED_CLASS_NAME];
object[PF_BLOCKED_USER] = [PFUser currentUser];
object[PF_BLOCKED_USER1] = user1;
object[PF_BLOCKED_USER2] = user2;
object[PF_BLOCKED_USERID2] = user2.objectId;
[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
if (error != nil) NSLog(@"BlockUserOne save error.");
}];
}
}
else NSLog(@"BlockUserOne query error.");
}];
}
此方法在被阻止的类中创建一个对象,其中包含作为阻塞进程一部分的两个用户,以及最初进行阻塞的用户的第三个字段。此方法被调用两次,user1
始终是当前用户:
PFUser *user1 = [PFUser currentUser];
BlockUserOne(user1, user2);
BlockUserOne(user2, user1);
这会在被阻止的类中创建两个对象(两个因为这会使两个用户都更容易成为阻塞过程的一部分)
现在,由于对象存在于被阻止的类中,因此在查询用户时需要检查它们。以下是使用搜索栏搜索用户的示例:
- (void)searchUsers:(NSString *)search
{
PFUser *user = [PFUser currentUser];
//Querying for users that have been blocked by the current user
//or have blocked the current user (therefore the two different
//objects that I created
PFQuery *query1 = [PFQuery queryWithClassName:PF_BLOCKED_CLASS_NAME];
[query1 whereKey:PF_BLOCKED_USER1 equalTo:user];
//We don't want to query the current user, don't we?
PFQuery *query2 = [PFQuery queryWithClassName:PF_USER_CLASS_NAME];
[query2 whereKey:PF_USER_OBJECTID notEqualTo:user.objectId];
//
//This is the crucial part!!
[query2 whereKey:PF_USER_OBJECTID doesNotMatchKey:PF_BLOCKED_USERID2 inQuery:query1];
//
//
[query2 whereKey:PF_USER_FULLNAME_LOWER equalTo:[search lowercaseString]];
[query2 orderByAscending:PF_USER_FULLNAME];
[query2 setLimit:1000];
[query2 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (error == nil)
{
//Do whatever you want to do
}
else //Handle the error
}];
}
首先,我们查询已阻止当前用户或已被当前用户阻止的所有用户,然后我们搜索第一个查询尚未返回的用户。
这会导致query2
返回尚未参与当前用户阻止过程的用户。
希望这有助于消除这种情况。