1

I've set up GameCenter turn-based matches for my game. Right now I'm just trying to display a list of the current matches for the current GK player.

Here's my code:

  GKTurnBasedMatch.loadMatchesWithCompletionHandler { (matchesOpt, errorOpt) in

    if let error = errorOpt {
      print("Error loading matches: \(error.localizedDescription)")
    } else if let matches = matchesOpt {
      self.matches = matches
      self.gamesTableView.reloadData()
    } else {
      print("Matches array is nil")
    }
  }
}

The third case is always hit, the matches array is nil.

I think there should be matches. I'm using

GKTurnBasedMatch.findMatchForRequest 

to start matches, and it is succeeding. Furthermore, if I note the matchId of the match from findMatchForRequest, and then I call

GKTurnBasedMatch.loadMatchWithID(matchId)

it finds the match, with the correct match data and everything.

Does anyone know how I can get loadMatchesWithCompletionHandler to return the same games that I can retrieve by id?

Edit: Here's the authentication code:

GKLocalPlayer.localPlayer().authenticateHandler = { (viewControllerOpt, errorOpt) in

  NSLog("GK local player authentication finished. Error: \(errorOpt)")
  if let viewController = viewControllerOpt {
    self.presentViewController(viewController, animated: true, completion: nil)
  }

  GKLocalPlayer.localPlayer().registerListener(self)

} 
Michael B
  • 11
  • 2
  • Can you add the code where you authenticate the local user? – Thunk Apr 08 '16 at 18:32
  • Another thing to check, in `findMatchForRequest` and `loadMatchWithID` completion handlers, try dumping the `participants` array to see what status the local player is in. – Thunk Apr 08 '16 at 18:47
  • Edit: Added authentication code. – Michael B Apr 09 '16 at 14:03
  • @Thunk, both immediately after the match is created, and also when it's retrieved with loadMatchWithID, the local player has status Active. The unmatched second player has status Matching. – Michael B Apr 09 '16 at 14:23
  • probably not related to this problem, but please note in your auth handler: when the error is set, the viewController will be nil. So, they way you have that coded, if an error occurs you will print a message then immediately attempt to register the unauthenticated listener. That's probably not the problem here, but could bite you later. For more info, see http://stackoverflow.com/a/35677324/1641444. – Thunk Apr 10 '16 at 07:24
  • Did you ever find an answer? – coopersita May 12 '16 at 02:00
  • I just tried it and @iPAT was right: the same code started working. – Michael B May 14 '16 at 12:22

1 Answers1

1

It seems multiple people encountered this recently. Not sure if it's relevant, but the examples cited have always been using swift. Based on How to list all available GKTurnBasedMatches for a player? it looks like just creating a leaderboard will clear the problem.

Based on the thread at https://stackoverflow.com/a/34056115/1641444, it appears that sometimes Game Center gets stupefied and, for whatever reason, creating a leaderboard/achievement, or even just changing the default, kick starts it back into action.

Community
  • 1
  • 1
Thunk
  • 4,099
  • 7
  • 28
  • 47
  • Thunk is right, this fixed it for me. Go to iTunes Connect > Your App > App Store tab > find your app version > toggle the Game Center switch and add a leaderboard. – Bokoskokos May 22 '16 at 17:28