GKTurnBasedMatch currentParticipant 在 saveCurrrentTurn 后变为 nil
GKTurnBasedMatch currentParticipant becomes nil after saveCurrrentTurn
我有一个方法,每当创建新的比赛时都会调用它(它设置游戏数据),它运行良好,除了有时(随机地)在我尝试保存数据后当前参与者变为 nil。
我设置了几个断点,直到我尝试保存初始游戏数据,currentParticipant
不是零,但保存后,有时是零:
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)
}
})
}
有什么想法吗?
尝试在保存程序的完成处理程序的顶部重新加载匹配对象。我知道这听起来很蹩脚。由于它发生 运行domly,我怀疑(又一个)GKTurnBasedMatch
错误。
但是,我 运行 在 Apple 文档的某处提到了匹配对象变得陈旧(and/or 从查询所有匹配列表中接收到不可靠的匹配对象,直到您实际调用 loadMatchWithID
为每个找到的匹配项),所以我最终变得非常自由,我使用 loadMatchWithID
作为使用 GKTurnBasedMatch
.
的必要成本
我有一个方法,每当创建新的比赛时都会调用它(它设置游戏数据),它运行良好,除了有时(随机地)在我尝试保存数据后当前参与者变为 nil。
我设置了几个断点,直到我尝试保存初始游戏数据,currentParticipant
不是零,但保存后,有时是零:
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)
}
})
}
有什么想法吗?
尝试在保存程序的完成处理程序的顶部重新加载匹配对象。我知道这听起来很蹩脚。由于它发生 运行domly,我怀疑(又一个)GKTurnBasedMatch
错误。
但是,我 运行 在 Apple 文档的某处提到了匹配对象变得陈旧(and/or 从查询所有匹配列表中接收到不可靠的匹配对象,直到您实际调用 loadMatchWithID
为每个找到的匹配项),所以我最终变得非常自由,我使用 loadMatchWithID
作为使用 GKTurnBasedMatch
.