0
votes

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]];
1
please describe the 'match' table, what types it's player 1 and 2 cols are (are they pointers to PFUsers?). What is 'myClass'? - danh
I tried to explain more in detail what I'm trying to do. col 1 and 2 are both strings. "myClass" was an error on my part. It should also be "match". - Ramin Afshar
Okay, that helps a lot, but there's one important question: what type are those player cols? pointers or strings? It seems from your description that they are strings. If so, that's a mistake that we'd need to fix. - danh
yes they are strings. it seemed easier just to store the id as a string instead of a pointer to PFUsers - Ramin Afshar

1 Answers

1
votes

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.