1

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?

4

1 回答 1

0

尝试在保护程序的完成处理程序顶部重新加载匹配对象。我知道这听起来很蹩脚。而且由于它是随机发生的,我怀疑(又一个)GKTurnBasedMatch错误。

但是,我在 Apple 文档中的某处提到了匹配对象变得陈旧(和/或从查询所有匹配项的列表中接收不可靠的匹配对象,直到您实际调用loadMatchWithID每个找到的匹配项),所以我最终变得非常自由我的使用loadMatchWithID作为必要的使用成本GKTurnBasedMatch

于 2016-11-26T01:06:25.427 回答