The MVI Architecture Pattern in Swift

Architect! With just RxSwift!

Steven Curtis
8 min readAug 13, 2020

--

Photo by KOBU Agency on Unsplash

You might already be familiar with MVVM even MVVM-C and even (jokes) MVC. But MVI? You might have heard of the Flux, Redux and MobX React patterns and you might not be aware that in native development this is actually MVI.

This article explores a sample project that uses **just** RxSwift for implementation. Wow. But the parts to make this work (while non-trivial) are not that hard…come on!

Difficulty: Beginner | Easy | Normal | Challenging

This article has been developed using Xcode 11.4.1, and Swift 5.2.2

Prerequisites:

  • Coding in Swift Playgrounds (guide HERE)
  • Some knowledge of RxSwift would be useful (guide HERE)

Terminology

MVI: Model-View-Intent

Redux: an open-source JavaScript library for managing application state

The Motivation

MVI is another architecture that looks at improving the separation of concerns when developing Apps. Those traditional patterns, MVC, MVVM and others (VIPER) are traditionally imperative. The issue is that reactive programming has become more popular, and there is nothing wrong with using RxSwift to create the bindings on MVVM.

In steps MVI, an architecture that is designed with reactive programming, where the model represents the state — the source of truth — of the application.

The Intent is an interaction that the user triggers and then carries out (often called a presenter, in architectures like MVP — which can be the ViewController in Apple’s standard iOS architecture).

In doing so, it makes code readable and extendable (through adding new states) and easy to debug by featuring a finite number of states.

The Basics

The View Observes the user actions and is responsible for visibly representing the model to the user.

--

--