我目前正以下列方式检索我的排行榜前100名得分:
- (void) retrieveTop100Scores {
__block int totalScore = 0;
GKLeaderboard *myLB = [[GKLeaderboard alloc] init];
myLB.identifier = [Team currentTeam];
myLB.timeScope = GKLeaderboardTimeScopeAllTime;
myLB.playerScope = GKLeaderboardPlayerScopeGlobal;
myLB.range = NSMakeRange(1, 100);
[myLB loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
if (scores != nil) {
for (GKScore *score in scores) {
NSLog(@"%lld", score.value);
totalScore += score.value;
}
NSLog(@"Total Score: %d", totalScore);
[self loadingDidEnd];
}
}];
}
问题是我想为32个排行榜做这个。我有一个包含所有32个排行榜标识符的数组:
NSArray *leaderboardIDs;
所以我的问题是,如何将这两段代码组合起来为每个排行榜提取100个值,从而生成一个包含所有排行榜名称作为键的字典,以及总计(每100个)得分的排行榜。
所以我使用CrimsonChris的帮助更新了我的答案。我唯一的问题是,我怎么知道所有32支球队的得分总和是什么时候?我问的原因是因为我想从最高到最低组织它们,并在tableView中按顺序显示它们。
这是我更新了我的答案:
在我的viewDidLoad中:
- (void)loadLeaderboardData {
// Array of leaderboard ID's to get high scores for
NSArray *leaderboardIDs = @[@"algeria", @"argentina", @"australia", @"belgium", @"bosniaandherzegovina", @"brazil", @"cameroon", @"chile", @"colombia", @"costarica", @"croatia", @"ecuador", @"england", @"france", @"germany", @"ghana", @"greece", @"honduras", @"iran", @"italy", @"ivorycoast", @"japan", @"mexico", @"netherlands", @"nigeria", @"portugal", @"russia", @"southkorea", @"spain", @"switzerland", @"unitedstates", @"uruguay"];
scoresByLeaderboardID = [NSMutableDictionary dictionary];
__block int requestCount = (int)leaderboardIDs.count;
for (NSString *leaderboardID in leaderboardIDs) {
[self loadScoresForLeaderboardID:leaderboardID range:NSMakeRange(1, 100) callback:^(NSArray *scores) {
scoresByLeaderboardID[leaderboardID] = scores;
if (--requestCount <= 0) {
if (callback)callback(scoresByLeaderboardID);
}
}];
}
}
- (void)loadScoresForLeaderboardID:(NSString*)leaderboardID range:(NSRange)range callback:(CallbackBlock)callback {
__block int totalScore = 0;
GKLeaderboard *myLB = [[GKLeaderboard alloc] init];
myLB.identifier = leaderboardID;
myLB.timeScope = GKLeaderboardTimeScopeAllTime;
myLB.playerScope = GKLeaderboardPlayerScopeGlobal;
myLB.range = range;
[myLB loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
if (error != nil) {
//NSLog(@"%@", [error localizedDescription]);
}
if (scores != nil) {
for (GKScore *score in scores) {
//NSLog(@"Individual Scores: %lld (For %@)", score.value, leaderboardID);
}
}
}];
}
答案 0 :(得分:1)
可以使用回调块清理您的方法。
typedef void(^CallbackBlock)(id object);
//callback accepts an NSArray* of GKScore*
- (void)loadScoresForLeaderboardID:(NSString *)leaderboardID range:(NSRange)range callback:(CallbackBlock)callback {
GKLeaderboard *myLB = [[GKLeaderboard alloc] init];
myLB.identifier = leaderboardID;
myLB.timeScope = GKLeaderboardTimeScopeAllTime;
myLB.playerScope = GKLeaderboardPlayerScopeGlobal;
myLB.range = range;
[myLB loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
if (error != nil) NSLog(@"%@", [error localizedDescription]);
if (callback) callback(scores);
}];
}
然后,您可以遍历排行榜。
//callback accepts an NSDictionary* of NSArray*(of GKScore*) by NSNumber*
- (void)loadScoresForLeaderboardIDs:(NSArray *)leaderboardIDs withCallback:(CallbackBlock)callback {
NSMutableDictionary *scoresByLeaderboardID = [NSMutableDictionary dictionary];
__block int requestCount = leaderboardIDs.count;
for (NSString *leaderboardID in leaderboardIDs) {
[LeaderboardLoader loadScoresForLeaderboardID:leaderboardID range:NSMakeRange(1, 100) callback:^(NSArray *scores) {
scoresByLeaderboardID[leaderboardID] = scores;
if (--requestCount <= 0) { //not thread safe
if (callback) callback(scoresByLeaderboardID);
}
}];
}
}
一旦此方法触发其回调,您应该获得所有分数。
[LeaderboardLoader loadScoresForLeaderboardIDs:@[@"FirstID", @"SecondID", @"ThirdID"] withCallback:^(NSDictionary *scoresByLeaderboardID) {
NSLog(@"%@", scoresByLeaderboardID);
//do whatever you need to with all your scores here.
}];