herzbube / littlego

Little Go. An iOS application that lets you play the game of Go on the iPhone or iPad.
https://littlego.herzbube.ch/
Apache License 2.0
138 stars 54 forks source link

Don't use KVO in GoBoardPosition to react to changes in GoNodeModel #402

Closed herzbube closed 1 year ago

herzbube commented 1 year ago

GoBoardPosition currently uses KVO to react to changes in GoNodeModel and to update its properties numberOfBoardPositions and currentBoardPosition. This "magic" is convenient, because clients that add or discard nodes to/from GoNodeModel do not need to also update GoBoardPosition. On the negative side, the updating logic triggered by KVO is difficult to understand and requires extensive documentation, because it involves assumptions about the behaviour of other parts of the code (at least GoGame, ChangeAndDiscardCommand and DiscardAndPlayCommand).

After various changes that were required to support game variations and board setup in multiple nodes, one specific error case was now found to exist:

After this analysis, it seems best to no longer use KVO in GoBoardPosition to react to changes in GoNodeModel. Although this removes some convenience, it also removes a difficult to understand piece of code and therefore improves overall maintainability.

herzbube commented 1 year ago

The following KVO-triggered updating logic in GoBoardPosition must be replaced:

The following actors must be changed accordingly:

herzbube commented 1 year ago

To avoid similar issues with an inconsistent model state in the future, all KVO observing of GoBoardPosition properties currentBoardPosition and numberOfBoardPositions should be replaced. Instead observers should react to notifications posted to the default notification centre, and the actors outlined in the previous comment should explicitly post those notifications when all Go model objects have received their correct state updates.

New notifications: