Ramin Afshar
Ramin Afshar

Reputation: 1019

PFQuery constraints - exclude subquery

I have a table called "match". this table has several columns of type string. I'm trying to create a query that queries for the string value "undefined" in column "player2". That works without problem. When I query for just that I get the results.

But I want to add a constraint so that it excludes the match objects where the current user's facebook id matches that of column "player1". I've stored the id in the PFUser object so i can easily retrieve it.

I'm creating a random player matching system where the query checks for an open spot in the match table's "player2" column. if it's "undefined" I know that there is an open slot in the match and the player can join it.

However it needs to exclude the matches the current player previously started himself. Otherwise it would join a match the current player started himself.

//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]];

Upvotes: 0

Views: 312

Answers (1)

danh
danh

Reputation: 62686

Your player1 and player2 cols are strings containing user ids. I think this is going to cause other problems, but you can still do the query you're attempting as follows...

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

EDIT - I changed fbId above to be the user's object id. That's what you're probably storing in the player string col. It's this kind of confusion that makes storing strings (instead of pointers) a problem for the design.

Upvotes: 1

Related Questions