问题描述
我目前正在通过以下方式检索我的一个排行榜的前100个得分:
I'm currently retrieving the Top 100 Scores for one of my leaderboards the following way:
- (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个排行榜标识符的数组:
The problem is I want to do this for 32 leaderboards. I have an array with all 32 leaderboard identifiers:
NSArray *leaderboardIDs;
所以我的问题是,如何将这两段代码组合起来拉入每个排行榜的100个值,导致所有排行榜名称都作为键的字典,以及值的排行榜的总计(每100个分数)。
所以我使用CrimsonChris的帮助更新了我的答案。我唯一的问题是,我怎么知道所有32支球队的得分总和是什么时候?我问的原因是因为我想从最高到最低组织它们,并在tableView中按顺序显示它们。
So I have updated my answer using CrimsonChris' help. The only question I have is, how can I know when it is done totaling the score for all 32 teams? The reason I ask, is because I would then like to organize them from highest to lowest, and display them in that order in a tableView.
这就是我的意思更新了我的答案:
This is what I've updated my answer to:
在我的viewDidLoad中:
In my 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);
}
}
}];
}
推荐答案
您的方法可以清理通过使用回调块。
Your method can be cleaned up by using callback blocks.
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);
}];
}
然后你可以遍历排行榜。
You can then loop over your leaderboards.
//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);
}
}];
}
}
一旦此方法触发其回调,您应该拥有所有得分。
Once this method fires its callback you should have all your scores.
[LeaderboardLoader loadScoresForLeaderboardIDs:@[@"FirstID", @"SecondID", @"ThirdID"] withCallback:^(NSDictionary *scoresByLeaderboardID) {
NSLog(@"%@", scoresByLeaderboardID);
//do whatever you need to with all your scores here.
}];
这篇关于iOS:最好的方式做这个没有调用方法32次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!