MVI is architectural pattern that separates an application into three main logical components Model, View, and Intent.
Model is responsible to manage data, that independent from the user interface. Additionally it holds a state of app or view. State is a data structure that view can observe.
Important! The View observes whole state, not the fields in the data structure of state. You can implement additional object called Reducer that creates a new immutable copy of the state from old state and new data.
View is responsible for rendering UI elements, accept user input, trigger events to change state of model and observe model.
Intent is an event that is received by the view for every user or app action. (Don't confuse with Android Intent class.)
- one immutable state common to all layers (i.e. single source of truth)
- unidirectional and cyclic data flow
- easy to test and fix bugs
1. Like in MVP the lifecycle of view is problem. This creates easy opportunities for memory leaks, bugs and crashes.
MVI vs others
You can think of MVI as an event-driven producer-consumer pattern.
MVP and MVVM may have multiple states that are holded in different variables. Therefore, developers need to synchronize state with the Observable and Observer callbacks. MVI has only one data structure that represents state.
MVI can be part of MVP or MVVM pattern. In this case Presenter or ViewModel will observe state of model.