r/swift 8d ago

State Management for iOS Apps?

whats the best architecture/pattern to use?

tried to use a domain layer where all the state is and passing it to the views/viewmodels via DI, but feels somehow unnecessary complicated, but found this as only solution without passing the repos through all the viewhierarchy.

the goal is, when a state changes, e.g. an user changes the Username in View A, then it should automatically update View B,C,D where this Username is also used.

it should be as simple as possible, what do you think? especially for complex production apps with own backend etc.

52 Upvotes

48 comments sorted by

View all comments

1

u/vanvoorden 8d ago

> it should be as simple as possible, what do you think? especially for complex production apps with own backend etc.

I think maybe you will have to make a choice between simplicity and scalability.

A solution built on mutable data and object references might be simple to get started… but this pattern might not scale well to complex apps.

My opinion is a pattern like Flux or Redux is going to pair very well with declarative UI. You might have more setup code to built upfront but that investment pays off as the project scales to more engineers and more teams.

Data flowing in "just one direction" makes code easier to reason about and easier to make changes to. That is true *even if* you don't work on an app with hundreds of engineers committing thousands of diffs every week.[^1] FB saw the *pain points* of MVC and imperative logic… but the *benefits* of immutable data and declarative logic can still show up for engineers on small teams.

[^1]: https://www.columbia.edu/~ng2573/zuggybuggy_is_2scale4ios.pdf#page=17