Reputation: 16338
How can I retrieve all scores for a GKLeaderboard? (I'm making my own leaderboard graphics). I'd need to know the number of scores in a certain leaderboard, but there seems to be no way of polling GC for that kind of information?
Upvotes: 1
Views: 1834
Reputation: 21785
I see that this question is over 10 years old now, so interfaces will have changed quite a lot in that time. I was interested in loading a set of scores from a leaderboard in iOS 14+ with the hope of being able to display the scores around the local player's own score, like how GKGameCenterViewController
does it. There are two reasons for wanting to do this:
After examining the (non-deprecated) functions of GKLeaderboard
, I came to the following conclusions:
loadEntries(for:timeScope:range:completionHandler:)
would seem to be the best function available.I tried supplying a range which broke the min/max rules and it caused a crash.
There is another loadEntries
function which lets you fetch scores for other players that you can identify. This could be used to fetch the scores of friends, but otherwise it doesn't help much.
So for iOS14+, the bottom line is: although it is at least possible to determine how many scores are in a leaderboard, it seems you can only fetch the local player's own score and scores in the top 100. See comments
Upvotes: 0
Reputation: 188
In order to get all the scores, you need to request them in chunks using the range that Scar details. Typically in games you display a subset of say 100, and give the user the option to see a lower subset or a higher subset.
Upvotes: 0
Reputation: 16338
I'm getting the feeling the answer is the maxRange property of GKLeaderboard. The docs:
This property is invalid until a call to loadScoresWithCompletionHandler: is completed. Afterward, it contains the total number of entries available to return to your game given the filters you applied to the query.
So this would be the number of rows in a leaderboard table view.
Upvotes: 1
Reputation: 3480
In the apple document, there is a section called Retrieving Leaderboard Scores, they use an example of how to retrive a top 10 score:
- (void) retrieveTopTenScores
{
GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
if (leaderboardRequest != nil)
{
leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal;
leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime;
leaderboardRequest.range = NSMakeRange(1,10);
[leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
if (error != nil)
{
// handle the error.
}
if (scores != nil)
{
// process the score information.
}
}];
}
}
We can edit the range of the leaderboardRequest
, to retrive more scores, apple said in there document:
range
The numerical score rankings to return from the search.
@property(nonatomic, assign) NSRange range
Discussion The range
property is ignored if the leaderboard request was initialized using the initWithPlayerIDs: method. Otherwise, the range property is used to filter which scores are returned to your game. For example, if you specified a range of [1,10], after the search is complete, your game receives the top ten scores. The default range is [1,25].
The minimum index is 1. The maximum length is 100.
Upvotes: 1