3

我需要对解析中的对象进行查询,以查看 2 个字段中的任何一个包含值。该值是一个 PFUser 对象。我想我需要某种 OR 语句,但我不知道该怎么做。

这是我到目前为止的代码..

  PFQuery *query = [PFQuery queryWithClassName:@"GameTradeObject"];

    [query whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];

    [query whereKey:@"fromUser" equalTo:[PFUser currentUser]];
    [query whereKey:@"toUser" equalTo:[PFUser currentUser]];

    [query includeKey:@"fromUser"];
    [query includeKey:@"fromVillage"];

    [query includeKey:@"toUser"];
    [query includeKey:@"toVillage"];

    [query findObjectsInBackgroundWithBlock:^(NSArray *PUObjects, NSError *error) {
        if (!error) {

我需要知道当前用户对象是否存在于 fromUser 或 toUser 字段中。我认为可能必须使用 NSPredicate 来完成,但我不知道如何构建语句。

4

3 回答 3

3

您可以使用[PFQuery orQueryWithSubqueries:@[query1, query2]]来组合两个查询。

  PFQuery *query1 = [PFQuery queryWithClassName:@"GameTradeObject"];
  [query1 whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
  [query1 whereKey:@"fromUser" equalTo:[PFUser currentUser]];

  PFQuery *query2 = [PFQuery queryWithClassName:@"GameTradeObject"];
  [query2 whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
  [query2 whereKey:@"toUser" equalTo:[PFUser currentUser]];

  PFQuery *query = [PFQuery orQueryWithSubqueries:@[query1, query2]];
  [query includeKey:@"fromUser"];
  [query includeKey:@"fromVillage"];
  [query includeKey:@"toUser"];
  [query includeKey:@"toVillage"];
  [query findObjectsInBackgroundWithBlock:...
于 2013-05-07T18:02:29.710 回答
0

您可以简单地实现以下方式;

  PFQuery *query1 = [PFQuery queryWithClassName:@"GameTradeObject"];
  [query1 whereKey:@"fromUser" equalTo:[PFUser currentUser]];
  [query1 whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
  PFQuery *query2 = [PFQuery queryWithClassName:@"GameTradeObject"];
  [query2 whereKey:@"toUser" equalTo:[PFUser currentUser]];
  [query2 whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
  PFQuery *query = [PFQuery orQueryWithSubqueries:@[query1, query2]];

而且您不需要第二次使用 includeKey,因为您将 query1 和 query2 过滤为 fromUser 和 toUser 列。

于 2014-09-15T10:49:36.280 回答
0

另一种通常执行速度更快的选项是将两个用户存储在一个数组中。你可能会考虑:

PFQuery *query = [PFQuery queryWithClassname:@"GameTradeObject"];
// array equalTo string returns all objects where the array contains the string
[query whereKey:@"users" equalTo:PFUser.currentUser];
[query includeKey:@"users"];
[query includeKey:@"villages"];
[query findObjectsInBackgroundWithBlock:...];
于 2013-05-08T17:16:18.780 回答