0

我有一个名为“匹配”的表。该表有几列字符串类型。我正在尝试创建一个查询来查询列“player2”中的字符串值“undefined”。这没有问题。当我查询时,我得到了结果。

但我想添加一个约束,以便它排除当前用户的 facebook id 与列“player1”匹配的匹配对象。我已将 id 存储在 PFUser 对象中,因此我可以轻松检索它。

我正在创建一个随机玩家匹配系统,其中查询检查匹配表的“player2”列中的空缺位置。如果它是“未定义”,我知道比赛中有一个空位,玩家可以加入。

但是,它需要排除当前玩家之前自己开始的比赛。否则它将加入当前玩家自己开始的比赛。

//check for objects that match the string "undefined"
PFQuery *query1 = [PFQuery queryWithClassName:@"match"];
[query1 whereKey:@"player2" containsString:@"UNDEFINED"];

//AND constraint to exclude the match of query1 if the current user id matches
PFQuery *query2 = [PFQuery queryWithClassName:@"match"];
[query2 whereKey:@"player1" notEqualTo:[[PFUser currentUser] objectForKey@"fbId"]];


PFQuery *mainQuery = [PFQuery orQueryWithSubqueries:@[query1,query2]];
4

1 回答 1

1

您的 player1 和 player2 cols 是包含用户 ID 的字符串。我认为这会导致其他问题,但您仍然可以按如下方式执行您尝试的查询...

NSString *userId = [PFUser currentUser].objectId;
NSPredicate *predicate = [NSPredicate predicateWithFormat:
                          @"(player1 = 'UNDEFINED') AND (player2 != %@)", userId];
PFQuery *query = [PFQuery queryWithClassName:@"match" predicate:predicate];

编辑- 我fbId在上面更改为用户的对象 ID。这就是您可能存储在播放器字符串 col 中的内容。正是这种混乱使得存储字符串(而不是指针)成为设计的一个问题。

于 2014-07-20T18:11:29.413 回答