Redux style iOS applications

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.

But now it all make sense, after using and developping with React on the web for a few weeks, it’s like I can’t really go back to my old ways of doing for native iOS development. I won’t switch to javascript (AKA React Native) for mobile application, but here is a few things I’ve learnt.

Being now back on iOS development, I’ve created a new project and began investigating ReSwift, it’s an implementation of the Flux and Redux pattern in Swift. And oh boys, it just works, I’ve somewhat cloned the architecture of our Javascript application, now I have a global state and my controllers are just listeners of those states. And those controllers are composed of various view components which encapsulates very specific behaviors.

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.

--

--

--

📱 🚀 🇫🇷 [Entrepreneur, iOS/Mac & Web dev] | Now @Medium, @Glose 📖| Past @google 🔍 | Co-founded few companies before, a movies 🎥 app and smart browser one.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Take Your React Skills to the Next Level in 4 Minutes

Angular vs React: Which JS Framework your Project Requires?

Configure less with ant design in React

iphone ses problemleri, iphone şebeke problemleri, iphone cep telefon tamiri

Google Map Integration with Polylines in Angular App

Three Methods for Animating Movement in JS/CSS

“Hacking” Facebook Messenger Games

Some JavaScript Fundamental Concepts

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thomas Ricouard

Thomas Ricouard

📱 🚀 🇫🇷 [Entrepreneur, iOS/Mac & Web dev] | Now @Medium, @Glose 📖| Past @google 🔍 | Co-founded few companies before, a movies 🎥 app and smart browser one.

More from Medium

Migrating users seamlessly from Codava/Kony App to Native/Flutter App —  Part1

How to add React Native to an existing iOS app in 2022

How to use Tink Link iOS with Expo

Native Vs Cross-Platform Mobile Application Development