1

I started with the friendsmash Facebook example and getting a score does not work if no score has previously been set. The code is below. If this is called it simply stalls at FBRequestConnection and does not produce an error or any result, no further code is executed in the block. Obviously this is unacceptable.

If I post the score without first checking its value, it posts fine, and then I can check its value correctly. I assume the call is not supposed to 'hang' if no score is set at all, or should I just not check the score and always post it?

void FB_Controller::FB_SendScore(const int nScore)
{
    // Make sure we have write permissions
    FB_RequestWritePermissions();

    NSMutableDictionary* params =   [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        [NSString stringWithFormat:@"%d", nScore], @"score",
                                        nil];

    NSLog(@"Fetching current score");

    // Get the score, and only send the updated score if it's highter
    [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"%llu/scores", m_uPlayerFBID] parameters:params HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

        if(error)
        {
            NSLog(@"ERROR getting score %@", error);
        }
        else if (result && !error) {

            int nCurrentScore = [[[[result objectForKey:@"data"] objectAtIndex:0] objectForKey:@"score"] intValue];

            NSLog(@"Current score is %d", nCurrentScore);

            if (nScore > nCurrentScore) {

                NSLog(@"Posting new score of %d", nScore);

                [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"%llu/scores", m_uPlayerFBID] parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

                    if(error)
                    {
                        NSLog(@"ERROR posting score %@", error);
                    }
                    else
                        NSLog(@"Score posted");
                }];
            }
            else {
                NSLog(@"Existing score is higher - not posting new score");
            }
        }

        }];

    // Send our custom OG
    //FB_SendOG();
}
4

1 回答 1

0

我刚遇到这个问题是:

当用户尚未向 Facebook 提交您应用的任何分数时,从 [result objectForKey:@"data"] 返回的字典为空。这会导致 objectAtIndex 引发异常。无论如何,我只是更换了:

int nCurrentScore = [[[[result objectForKey:@"data"] objectAtIndex:0] objectForKey:@"score"] intValue];

和:

int nCurrentScore = 0;
if ([[result objectForKey:@"data"] count]) {
    nCurrentScore = [[[[result objectForKey:@"data"] objectAtIndex:0] objectForKey:@"score"] intValue];
}
于 2014-06-02T22:05:08.500 回答