External Data with CompletionHandler not working

I have been working through some code and I would like to work out how to get around this problem with the GameCenter GameKit.

There is some example code (now open to the public, no longer pre-release so I can talk about it).

@synthesize playerStorage;

- (void)loadPlayerData:(NSArray *)identifiers
{
    [GKPlayer loadPlayersForIdentifiers:identifiers withCompletionHandler:^(NSArray *players, NSError *error) {
        if (error != nil
        {
            // Handle the error.
        }
        if (players != nil)
        {
            // Process the array of GKPlayer objects.
            // If I put this array in one I have created for the ViewController
            // it doesn't actually store it. It's like it can't find it.
            // But it doesn't error.
            [self setPlayerStorage:players];

            // Then animate to show why I need to do it this way, I cannot pass data
            [UIView beginAnimations:@"fadeInPlayer" context:self];
            [UIView setAnimationDuration:1.0];
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDidStopSelector:@selector(fadeInFriend:finished:context:)];
            labelPlayer.alpha = 0.0;
            [UIView commitAnimations];
        }
     }];
}
- (void)fadeInFriend:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    // Try and get the contents of playerStorage and update the label
    labelPlayer.text = [[playerStorage objectAtIndex:0] alias];

    // Fade in player label but label is blank
    // This is all example data, I'm doing it differently in the actual app
}

As you can see above, there is a "Completion Handler" and basically, I want to be able to take data from the completion handler and store it while I run some animations. For example, fade out current data and then fade in new data that was returned from the completion handler. But it doesn't let me.

I try to store the information in an NSArray (NSDictionary in the actual code), but when I call the keys from another function the information is blank.

Any ideas how to get round this?


I did something similar, but I just wrote elements (eg, alias) from the returned "players" to my array, worked fine - I think you may be passing only a reference to players to your array, so you're just seeing "players". My code (with "highScores" already created from loadScoresWithCompletionHandler) -

for (int i = 0; i < [players count]; i ++)
                      {
                          NSMutableDictionary *thisEntry = [highScores objectAtIndex:i];
                          [thisEntry setObject:[[players objectAtIndex:i] alias] forKey:@"alias"];
                      }

Anyway, worked for me, if you'd like the whole chunk of code let me know.

链接地址: http://www.djcxy.com/p/51790.html

上一篇: iPhone的全部地址簿数据

下一篇: CompletionHandler的外部数据无法使用