Practical State Machines with GameplayKit | raywenderlich.com

In this tutorial, you’ll convert an iOS app to use a state machine for navigation logic using GameplayKit’s GKStateMachine.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3685223-practical-state-machines-with-gameplaykit

Since this is just an example to show how someone would use GKStateMachine for Non-Game Apps it delivers the point, but I don’t think someone should slam it on the coordinator pattern since the GKStateMachine approach is the coordinator pattern itself.

Its a bit confusing since the coordinators in this case actually just encapsulate a few properties and trigger the navigation; a task that could be done by the state itself. I’m ok with encapsulating but having a State Machine (i. e. a coordinator with transition validation) and coordinators (without validation) is a bit redundant and confusing for new people coming to the project.

I think GKStateMachine is a very powerful coordinator and very much under-utilised. Unfortunately :frowning:

@keeganrush Do you have any feedback about this? Thank you - much appreciated! :]

That’s a good point. I personally like to use GKStateMachine for either:

  • Complex state validation logic
  • State that might span across more than one class or drives the overarching behavior of the app
  • Business logic that depends on both the prior state and the next state in a state transition

Thanks for your comment!

This tutorial looks promising, but too bad, after adding state machine, it doesn’t work properly after clicking back button on the navigation bar. It doesn’t handle popping on navigation controller.

For example, opening the app, click on a kanji, then click back button on navigation bar. Now you got stuck on that state.

NavigationController has its own state. How would you sync it with KanjiStateMachine?

@keeganrush Can you please help with this when you get a chance? Thank you - much appreciated! :]

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!