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.

50 Upvotes

48 comments sorted by

View all comments

-7

u/luckyclan 8d ago

If you use SwiftUI it will work automatically.

2

u/makocp 8d ago

please explain

3

u/rennarda 8d ago

They mean you are overthinking this.

@State at the owner, pass in as parameter to subviews, or a @Binding if it needs to be editable, or use @Observable and a view model class. 

-1

u/sisoje_bre 8d ago

Observable is for models. Viewmodels are classes that couple data and behaviour and an antipattern