Redux-like state container in SwiftUI. State normalization.
Tags: swift, ios, mobile, programming
Published: 2023-02-18 11:36:00

`Redux` stores the whole app’s state as a single source of truth. It allows us to keep our `User Interface` in sync with the app state. But to achieve this, we have to normalize our state. Let’s take a look at the example.

```swift
struct AppState {
    var allTasks: [Task]
    var favorited: [Task]
}
```
Here we have an `AppState` struct which stores a task list and favorited tasks. It looks straightforward, but it has one big downside. Assume that you have the edit task screen where you can modify the selected task. Whenever the user hits the save button, you have to find and update a particular task in the allTasks list and favorited list. It can be error-prone and lead to a performance issue as soon as you have a long list of tasks.

Let’s improve performance by normalizing our state struct. First of all, we need to store our tasks in `Dictionary` where task id is the key and task itself is the value. `Dictionary` can retrieve the value by key in constant **(O(1))** time, but it doesn’t keep the order. We can create an array with ids to save the order. Let’s take a look at the normalized version of our state.

```swift
struct AppState {
    var tasks: [Int: Task]
    var allTasks: [Int]
    var favorited: [Int]
}
```
As you can see in the example above, we store our tasks in the `Dictionary` where task `id` is the key, and the task is the value. We also store arrays of identifiers for all tasks and favorited ones. By using identifiers instead of copies, we achieve a centralized state persistence which keeps our `User Interface` and data in sync.

---

**Redux** provides a single source of truth, which eliminates tons of bugs produced by multiple states across the app. Best practices. Normalization and composition keep our app state simple and maintainable.

- [State normalization](https://dev.to/sergeyleschev/redux-like-state-container-in-swiftui-state-normalization-108o).
- [State composition](https://dev.to/sergeyleschev/redux-like-state-container-in-swiftui-state-composition-57e5).
- [Reducer composition](https://dev.to/sergeyleschev/redux-like-state-container-in-swiftui-reducer-composition-3j0m).
- [Derived stores](https://dev.to/sergeyleschev/redux-like-state-container-in-swiftui-derived-stores-2d62).

---

**Contacts**
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: sergey.leschev@gmail.com
👋 LinkedIn: https://linkedin.com/in/sergeyleschev/
👋 LeetCode: https://leetcode.com/sergeyleschev/
👋 Twitter: https://twitter.com/sergeyleschev
👋 Github: https://github.com/sergeyleschev
🌎 Website: https://sergeyleschev.github.io
🌎 Reddit: https://reddit.com/user/sergeyleschev
🌎 Quora: https://quora.com/sergey-leschev
🌎 Medium: https://medium.com/@sergeyleschev