Stuart Breckenridge

Match Data in GameKit

Over the last few days I’ve started working with the GameKit API for the first time in a few years. It’s as bad as I remember.

The most egregious problem I have come across is this particular API:

GKTurnBasedMatch.loadMatches { (matches, error) in 
    if matches != nil {
	    // Do something with the matches
    }
}

This is the API description:

Loads the turn-based matches involving the local player and creates a match object for each match.

And this is the description of the matches parameter:

An array of GKTurnBased Match objects containing the match objects for matches that the local player is playing in, or nil if there are no matches to load.

This is hardly the full story. The loadMatches API will indeed download matches from Game Center, but what this description lacks is any reference (at all) to the fact that the downloaded matches will very likely include stale or non-existent matchData.1

As I encode the match score within matchData, I ran into issues when Game Center was downloading out-of-date matchData. This necessitated in a second call to either of the following functions before displaying data to the user:

GKTurnBasedMatch.load(withID: match.matchID!, withCompletionHandler: { (match, error) in 
    // Logic
}

Alternatively:

match.loadMatchData(completionHandler: { (data, error) in 
    // Logic
})

In my opinion, the loadMatches API should return the current matches the user is participating in with the latest matchData.

  1. I’ve created rdar://34782360 in the hope that Apple will clear up this description in a future update. ↩︎