I have 2 PFObjects - a Player let's say, and PlayerStats. There are also several other PFObjects associated with a PFPlayer.
I want to list the Players with the highest scores. The score is contained in PlayerStats.
Now I've tried:
PFQuery *andQuery = [PFQuery queryWithClassName:@"Player"];
[andQuery includeKey:@"playerStats"];
PFQuery *statsQuery = [PlayerStats query];
[statsQuery orderByDescending:@"score"];
[andQuery whereKey:@"miniStats" matchesQuery:statsQuery];
But this doesn't appear to respect the order that the stats are returned in. What I really want to do is order my andQuery by a value in its included class.
I've tried running a basic query and ordering the results afterwards, however I have over 999 players, which is the limit of a PFQuery, so that doesn't help.
I suspect I have to do 2 levels of PFQuery - one for PlayerStats, in order, and then on the results of that query, submit another query using the objectIds of the players to get the Player objects. But this seems cumbersome, especially if I'm only searching for 20 results at a time say.
Ideally there would be something like:
[andQuery orderByDescending:@"playerStats.score"];
We wish, but no, not supported. The only approach is to sort the result. At least underscorejs is concise and awesome...
var _ = require('underscore');
PFQuery *andQuery = [PFQuery queryWithClassName:@"Player"];
[andQuery includeKey:@"playerStats"];
andQuery.find().then(function(players) {
players = _.sortBy(players, function(player) {
return player.get("playerStats").get("score");
});
});
EDIT - I should have mentioned initially: if PlayerStat has a pointer to Player (which would be sensible), then you can do the query on stats for things like the all-time high score...
PFQuery *andQuery = [PFQuery queryWithClassName:@"PlayerStat"];
[andQuery includeKey:@"user"];
[andQuery descending:@"score"];
And at least the dot notation works for sub-subordinate keys (3 deep is the max)...
[andQuery includeKey:@"user.somePointerOnUser"];
As with all queries, these are limited to a max of 1000.