Does TCA really meant to be a single state/store per application? #1123
-
Hi, We are in the middle of some R&D on TCA and the perception is that TCA encourages to use single store/state in the application instead of local states/stores per feature? If that is true then:
Thank you for your input -Shahzad |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
TCA applications are indeed meant to be run off a single store, but we don't force that in anyway, so you are free to come up with your own patterns if you feel the need. It's worth noting that although it is true that a single store powers the entire application, it is not true that every single view in the entire application holds onto that store. In reality you use tools like Further, by using |
Beta Was this translation helpful? Give feedback.
-
Your app may have 50 features, but are all 50 features active at once? If they are, then you are going to have memory problems regardless if you have 1 store with 50 features or 50 stores with 1 feature. In practice, an app with 50 features does not have all 50 active at once. Say you have a tab bar application with 4 tabs, and each tab is a feature. That means when the application first starts you have 4 active features. Then say the first tab has a drill down to another feature. That feature is not active until you actually drill down. You can model that with optional state so that it is Then say that drill down screen has a feature that can appear in a presentation modal. That feature too can be modeled with optional state so that it is If you keep going down this route you will see that your application state is a tree of nested optionals and enums. Rather than thinking of app state like this: struct AppState {
var feature1: Feature1
var feature2: Feature2
var feature3: Feature3
var feature4: Feature4
var feature5: Feature5
...
var feature50: Feature50
} What you really have is more like this: struct AppState {
var tab1: Tab1
var tab2: Tab2
var tab3: Tab3
var tab4: Tab4
}
struct Tab1 {
var drillDown: DrillDown?
...
}
struct DrillDown {
var modal: Modal?
...
}
struct Modal {
...
}
struct Tab2 {...}
struct Tab3 {...}
struct Tab4 {...} In order to achieve this you need to make use of all the domain modeling tools that come in TCA. They allow you to use optionals and enums to create a tree of state, and then you use the reducer and store operators to compose those things.
State that needs to be globally accessible should be modeled in the environment, not in state. You think think of it more like database access or user defaults access. There is a new discussion about this topic (here) and I've previously discussed it on the swift forums (here). |
Beta Was this translation helpful? Give feedback.
Your app may have 50 features, but are all 50 features active at once? If they are, then you are going to have memory problems regardless if you have 1 store with 50 features or 50 stores with 1 feature.
In practice, an app with 50 features does not have all 50 active at once. Say you have a tab bar application with 4 tabs, and each tab is a feature. That means when the application first starts you have 4 active features.
Then say the first tab has a drill down to another feature. That feature is not active until you actually drill down. You can model that with optional state so that it is
nil
until activated and its view is not shown until it is activated.Then say that drill down screen…