iPhone Developement : Why does GKLeaderboard loadScoresWithCompletionHandler: return a null value
I'm working With Game Center right now and I have a issue with GC. When I'm using initWithPlayerIDs:, I don't get any score when loadScoresWithCompletionHandler: callback is called.
GKLeaderboard *leaderBoard = [[[GKLeaderboard alloc] initWithPlayerIDs:[NSArray arrayWithObject:gcPlayerID]] autorelease];
leaderBoard.timeScope = GKLeaderboardTimeScopeAllTime;
leaderBoard.category = @"SomeLeaderboard";
[leaderBoard loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
if (error == nil)
{
// scores is null
// ...
}
But when I'm doing:
GKLeaderboard *leaderBoard = [[[GKLeaderboard alloc] init] autorelease];
leaderBoard.timeScope = GKLeaderboardTimeScopeAllTime;
leaderBoard.category = @"SomeLeaderboard";
[leaderBoard loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
if (error == nil)
{
for (GKScore* score in scores)
if ([score.playerID isEqualToString:gcPlayerID])
{
// Got something here
return;
}
}
It's working.
I'm using the 2nd method at the moment but it will make time to process if there is m开发者_如何学编程any score.
Does anyone have the same issue ?
Thanks.
I'm afraid that with the given info I don't have an answer as to why initWithPlayerIDs:
is not working. However, I might be able to simplify your filtering for the local player score in the 2nd method. A GKLeaderboard
has a property localPlayerScore
that is valid only after loadScoresWithCompletionHandler:
has completed. localPlayerScore
then gives the GKScore
for the local player. Your 2nd method would then look like this:
[leaderBoard loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
if (error == nil)
{
GKScore* myScore = leaderboard.localPlayerScore;
}
}
Hope this helps a little.
Try to narrow the type of scores to those you really want to process. For example request the score for the logged in player and for global time scope etc.
I am using something like the following code snippet in my own game and it is fast:
// Load score for player
GKLeaderboard *board = [[GKLeaderboard alloc] initWithPlayerIDs:[NSArray arrayWithObject:myGCPlayerID]];
board.timeScope = GKLeaderboardTimeScopeAllTime;
board.playerScope = GKLeaderboardPlayerScopeGlobal;
board.category = @"myGCCategory";
[board loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
if(error != nil) {
NSLog(@"Error loading score:\n%@", [error localizedDescription]);
}
if(scores != nil) {
for(int i=0; i<[scores count]; i++) {
GKScore *score = (GKScore *)[scores objectAtIndex:i];
if(([score.playerID isEqualToString:myGCPlayerID]) &&
(score.value > playerLastScore)) {
playerLastScore = score.value;
}
}
}
}];
[board release];
You may also apply a more optimized score loop as it is in your code. I hope this helps.
精彩评论