11

I'm creating a Game Center game with GKTurnBasedMatch matches. I'm having a problem where the readonly matchData property on a GKTurnBasedMatch does not seem to be properly stored on Game Center servers.

I'm using this StackOverflow answer to generate an md5 checksum on the matchData NSData, both when being sent and received to and from the Game Center servers.

I note the checksum of my NSData game data object when I send the matchData using the GKTurnBasedMatch instance method endTurnWithNextParticipants:turnTimeout:matchData:completionHandler:.

The opponent then retrieves the matches using GKTurnBasedMatch's class method loadMatchesWithCompletionHandler:, and when the matches arrive (no errors), I note the checksum again.

The two checksums do not match, and the resulting data are clearly not identical based on the reconstructed game. I have checked in the two accounts that the matchID property on my GKTurnBasedMatch objects are identical.

I've also performed the following test:

NSLog(@"matchID: %@ matchData checksum: %@",
                    match.matchID, 
                    [Utilities md5StringFromData:match.matchData]);

// match is a valid `GKTurnBasedMatch` object.
[match endTurnWithNextParticipants: @[ opponent ] // My `GKTurnBasedParticipant` opponent
                       turnTimeout:600
                         matchData:data // This is a valid NSData object
                 completionHandler:^(NSError *error) {
                      if (nil != error) {

                          NSLog(@"%@", error);

                      } else {

NSLog(@"Successfully ended turn.");

[GKTurnBasedMatch loadMatchesWithCompletionHandler:^(NSArray *matches, NSError *error) {

    if (nil != error) {

        NSLog(@"Error getting matches: %@", [error localizedDescription]);

    } else {

        for (GKTurnBasedMatch *match in matches) {

            NSLog(@"matchID: %@ matchData checksum: %@", 
                        match.matchID, 
                        [Utilities md5StringFromData:match.matchData]);

        }

    }
}];

                      }
}];

In this sample, where I end the turn with data and immediately retrieve the matches from Game Center, the data match. However, when I access the matchData from the opponent's Game Center account and device, they differ.

Anyone encountered anything like this?

Community
  • 1
  • 1
Tim Arnold
  • 8,359
  • 8
  • 44
  • 67

1 Answers1

11

I discovered the solution on Apple's Dev Forums.

It turns out that loadMatchesWithCompletionHandler: doesn't always grab the most up-to-date matchData. To make sure you have the most recent version, make sure you call the loadMatchDataWithCompletionHandler: method on your GKTurnBasedMatch object.

Mohsin Khubaib Ahmed
  • 1,008
  • 16
  • 32
Tim Arnold
  • 8,359
  • 8
  • 44
  • 67
  • Do you have any idea what the reasoning is behind this implementation? – Paul Hunter Apr 13 '13 at 11:34
  • I assume the rationale is that they'd like to minimize network requests and do optimal caching, but man, this was driving me insane until I found the solution. – Tim Arnold Apr 15 '13 at 15:41
  • It seems like broken caching to me. Even the documentation does not provide any notice of the fact that the game objects may not be current. I was also going nuts over this. – Paul Hunter Apr 16 '13 at 09:19
  • The docs don't explicitly mention these possible stale objects, but if you search for these methods in the Game Center Programming Guide, it seems maybe a tiny bit plausible that what they wrote corresponds to how it works. But yeah, I'm with you, should have been made more clear. – Tim Arnold Apr 16 '13 at 13:14
  • hi, When I get Take turn value. the values of previous match is over ride with the current match. how can I solve this . – Vineesh TP Jun 14 '13 at 16:04
  • Vineesh TP: I'm not sure I understand your question! – Tim Arnold Jun 19 '13 at 19:26