gkturnbasedmatch

GKTurnBasedMatch currentParticipant becomes nil after saveCurrrentTurn


I have a method that gets called whenever a new match is created (it setups the game data), and it works well, except sometimes (randomly) the current participant becomes nil after I try to save the data.

I've placed a couple of breakpoints, and up until I try to save the initial game data, currentParticipant is not nil, but after saving, it's sometimes nil:

func enterNewGame(_ match:GKTurnBasedMatch) {
    self.match = match
    var pArray = [Player]()

    let mode: Game.Mode = .quick

    self.game = Game(mode: mode, players: pArray)

    if match.participants != nil {
        for (index, player) in match.participants!.enumerated() {

           //populate the pArray with Players, with corresponding initial data.
        }


    }

    // More setup to the Game object here.


//At this point, match.currentParticipant is not nil

    let data = NSKeyedArchiver.archivedData(withRootObject: game!)
    match.saveCurrentTurn(withMatch: data, completionHandler: {error in
        if error != nil {
            print(error!.localizedDescription)
            return
        }

        if self.segueToPick != "" {
//At this point, match.currentParticipant is sometimes nil
            self.performSegue(withIdentifier: self.segueToPick, sender: self)
        }

    })


}

Any ideas?


Solution

  • Try reloading the match object at the top of the saver's completion handler. I know that sounds lame. And since it happens randomly, I suspect (yet another) GKTurnBasedMatch bug.

    But, I ran across a mention somewhere in Apple's docs about the match objects becoming stale (and/or receiving unreliable match objects from querying the list of all matches until you actually call loadMatchWithID for each found match), so I eventually became very liberal with my use of loadMatchWithID as a necessary cost of using GKTurnBasedMatch.