Instance Method

loadMatchData(completionHandler:)

Loads the game-specific data associated with a match, including all exchanges.

Declaration

func loadMatchData(completionHandler: ((Data?, Error?) -> Void)? = nil)

Parameters

completionHandler

A block to be called after the match data is retrieved from the server.

The block receives the following parameters:

matchData

The data stored on Game Center that reflects the current state of the match. If an error occurred, this value is nil.

error

If an error occurred, this error object describes the error. If the operation was completed successfully, the value is nil.

Discussion

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread. Listing 1 shows a typical implemenation of this method.

Listing 1

Loading the match data from Game Center

- (void) loadAndDisplayMatchData
{
    [this.myMatch loadMatchDataWithCompletionhandler: ^(NSData *matchData, NSError *error) {
        if (matchData)
        {
// App-specific routine to decode the match data.
            this.gameData = [MyMatchDataClass initWithData: matchData];
            // Display the match.
        }
      }];
}