DEV Community

Cover image for Redux-like state container in SwiftUI. Unidirectional flow.
Sergey Leschev
Sergey Leschev

Posted on • Updated on

Redux-like state container in SwiftUI. Unidirectional flow.

...about data flow now.

Every view has a read-only access to the state via store object. Views can send actions to the store object. Reducer modifies the state, and then SwiftUI notifies all the views about state changes. SwiftUI has a super-efficient diffing algorithm thatโ€™s why diffing of the whole app state and updating changed views works so fast. Letโ€™s modify our store object to support sending actions.

final class Store<State, Action>: ObservableObject {
    @Published private(set) var state: State

    private let reducer: Reducer<State, Action>

    init(initialState: State, reducer: @escaping Reducer<State, Action>) {
        self.state = initialState
        self.reducer = reducer

    func send(_ action: Action) {
        reducer(&state, action)
Enter fullscreen mode Exit fullscreen mode

State -> View -> Action -> State -> View

This architecture revolves around a strict unidirectional data flow. It means that all the data in the application follows the same pattern, making the logic of your app more predictable and easier to understand.

I have a clear focus on time-to-market and don't prioritize technical debt. And I took part in the Pre-Sale/RFX activity as a System Architect, assessment efforts for Mobile (iOS-Swift, Android-Kotlin), Frontend (React-TypeScript) and Backend (NodeJS-.NET-PHP-Kafka-SQL-NoSQL). And I also formed the work of Pre-Sale as a CTO from Opportunity to Proposal via knowledge transfer to Successful Delivery.

๐Ÿ›ฉ๏ธ #startups #management #cto #swift #typescript #database
๐Ÿ“ง Email:
๐Ÿ‘‹ LinkedIn:
๐Ÿ‘‹ LeetCode:
๐Ÿ‘‹ Twitter:
๐Ÿ‘‹ Github:
๐ŸŒŽ Website:
๐ŸŒŽ Reddit:
๐ŸŒŽ Quora:
๐ŸŒŽ Medium:

Top comments (0)