Redux style iOS applications
With code inside
Lately I’ve been doing more frontend development than mobile development and I’ve come across some very interesting design patterns I was already aware, but didn’t really digged into it… until now.
All mutations of the state are done in the same place, in the reducer. One per substates. You can see all actions in one place. No more networking code or call in controllers, no more object mutation in views. No more spaghetti code. You have one state and it’s the truth, then your different view components (and I insist on that) subscribe to various part of your state and react accordingly. It’s just a plain better architecture for an application which have a strong model layer.
I used to have my Login view controllers bloated with ton of line of code, different state to manage, error handling etc… Now it looks like this: (As an example)
Your controllers and views dispatch actions to the global state, those actions actually perform the network or trigger the various parts your app will need to mutate to its new state.
An action can trigger another action, this is what happen for network query for example, you have one action FetchUser(id: String) and one action that you’ll intercept in your reducer which look like this SetUser(user: User). In the reducer you’re responsible to merge your new object with your current state.
First you need a state, my example will be centered around User object, so your state could look like this:
For should have a file which encapsulate all your network actions for your User object
Once the request is done it call another Action, this action is actually empty, it should be referenced in a UsersActions for example. This action describes your result, what does your reducer should rely on in order to mutate your state.
And finally, the most important works is done in the UsersReducer, you’ll catch your action and do your stuff accordingly to its content:
Now all you need is to suscribe to your state in your controllers or views and when it changes, extract the information you need and boom, new values for you!
But you should look at the ReSwift examples for deeper explanations, I plan to publish an open source application (a game actually) using that design pattern. But for now the code in this article is just to give you a very rough idea of how it work together.
It’s still a very early architecture for us at Glose books but we can’t wait to have an application in production using it.
I feel like developing applications using this pattern will save time and energy. It’s a bit of more work than a stupidly simple REST client, because your client state will have a bit more logic inside, but in the end it’ll save you invaluable debugging time. You’ll be able to mutate a lot of things locally, and no more cascading changes across your controllers and views. Replay state in the reserve order, archive it, create middleware etc… The data flow of your application is clear, centralised and simple.
The Redux pattern adds a bit of structure to your application. I’ve been doing pure MVC for a very long time, sure you can make a clean codebase, but you tend to develop habits which often do more harm than good. You can even go one step further and go full Redux by managing your UI (such as presenting controllers, presenting alerts, routing controllers) in a separate state, but I’m not there yet).
And tests… Unit Testing is now easy, because all you have to test is an input against your state, and your state is centralised, so your tests can dispatch mock actions, and then check if the state of your application conform to what you would except from that action.
Seriously, it’s the future.