Redux is a progression of the Flux application architecture for web apps from Facebook. In the center of both Flux and Redux is the unidirectional data flow.
This blog describes how Redux can be used a an iOS application architecture.
I have created a simple Redux implementation for iOS with a sample Swift app. The source code is on github.
One app state
There big difference to any other app architecture is that Redux has only one state for the whole app.
The app state is kept in the Store.
The state can be be changed form anywhere and will update any UI that subscribed to reflect the new app state.
Action are used to trigger the transfer of the app state to a new state.
Listeners get updated whenever the app state changes.
The power of Redux is in the Reducers which are used to transfer app state. Reducers can be composed. An single app reducer is configured in the Store which may then call other reducers to transfer parts of the app state. This is a great way to decompose state transition and keep code organised.
Unidirectional data flow
Data flows only in one direction. When the UI changes it sends an Action to the Store via a Dispatch method.
The app state gets transferred into a new state by calling the app reducer which then calls various reducers and composes the new app state.
After a state change all listeners get notified. A view controller would for example subscribe to state changes and it's update method gets called which in turn updates the whole UI (or View Model if MVVM is used)
- An Action is created and dispatched to the Store. Actions may be created by view controllers, data sources anything that wants to change the state of the app.
- The Store invokes the app Reducer with it’s current state and the Action.
- The app Reducer calls other reducers to handle the Action.
- The other Reducers return their state change.
- The app Reducer combines the states from all reducers into a new state and the Store updates the App State.
- The Store notifies all Subscribers about the new App State. Subscribers will most likely be View Controllers who then update their views to reflect the new App State.
- View controllers may create a new Action to reflect any user interaction and calls dispatch on the Store. Other objects may also creata action e.g. a data sources may create an action. Data received on a web socket could also trigger the creation of an Action to reflect changes.
And the cycle repeats.
Sample iOS App
The sample app demonstrates how Redux is used and what problem it elegantly solves. The sample app has a settings view controller that shows two settings which can be toggled on or off. The user can toggle facebook which will create a toggle action.
To demonstrate the power of Redux another screen has a plain old button which creates the same action to toggle facebook. No matter which screen changes the setting the other screen will be updated - nice.
Where from here
ReSwift is a nice framework that implements Redux for iOS. ReSwift let's you even time travel. As the state is managed centrally, it's not too hard to keep track of all states over time. You can even use this to move your app between the different states over time.
If you want a super simple Redux implementation for iOS then use the Redux.swift file in the sample app.
Redux is pretty simple and you can implement your own Swift version of Redux. Check out the excellent videos from Dan Abramov who has created Redux.