These days, I’ve been working on a feature like implementing sign up form detail pages which I don’t want to write into database until the flow is complete. Yet I use MVVM pattern for my project which of course is a quite simple and clean architecture, accumulating intermediate state like this leads to singleton abuse and other problems.

These arise from multiple copies of the state and if you allow your app to navigate back-and-forth, then you have to make sure that the latest state in the latest view controller is available in other view controllers as well. Maintaining the state across all of your controllers that are involved through delegation or callbacks becomes messy. I suppose that kind of problem is quite common among iOS developers.

Image for post

In this case, I chose to integrate Unidirectional Data Flow (Redux) approach with MVVM in my implementation. In the post, I try to explain** work flow of unidirectional data **flow(redux) and **how we can use it for time traveling of state in our app. **Woo hoo! Let’s start… .

Unidirectional data flow_ is an essential feature for working with the web-based applications._

The reason data flows from top to bottom is that change detection strategies are also always performed from top to bottom for every single component, every single time, starting from the root component. This is awesome, as unidirectional data flow is more predictable than cycles.

So What is Redux and how does it work?

Redux is an alternative or a variation of the flux framework that was developed at Facebook, and that Facebook now uses for most of their web applications. The main idea is that information always only flows in one single direction.

Image for post

The unidirectional data flow of redux

Let’s break down a bit what each component does for our implementation, shall we…

  • Views: Subscribe to Store changes and display them on screen. Views send Actions.
  • Actions: Actions don’t contain any code and are small pieces of data that describe a state change. An Action is manipulated by a Reducer.
  • Reducers: Provide pure functions that based on the current action and the current app state, and return a new app state.
  • Store: Stores the current value of the application state. Other modules like Views can subscribe and react to its changes.

In this article, I’ll use Reswift to implement unidirectional data flow. Reswift is a framework that helps you create Redux-like architecture in swift.

#redux #unidirectional-data-flow #ios-app-development #reswift #ios

The Unidirectional Data Flow and Time Traveling of States in iOS
2.65 GEEK