Sources/ComposableArchitecture/Documentation.docc/Articles/MigrationGuides/MigratingTo1.19.md
Store internals have been rewritten for performance and future features, and are now compatible with
SwiftUI's @StateObject property wrapper.
There are no steps needed to migrate to 1.19 of the Composable Architecture, but there are a number
of changes and improvements that have been made to the Store that one should be aware of.
The store's internals have been rewritten to improved performance and to pave the way for future features. While this should not be a breaking change, with any rewrite it is important to thoroughly test your application after upgrading.
SwiftUI's @State and @StateObject allow a view to own a value or object over time, ensuring that
when a parent view is recomputed, the view-local state isn't recreated from scratch.
One important difference between @State and @StateObject is that @State's initializer is
eager, while @StateObject's is lazy. Because of this, if you initialize a root Store to be held
in @State, stores will be initialized (and immediately discarded) whenever the parent view's body
is computed.
To avoid the creation of these stores, one can now assign the store to a @StateObject, instead:
struct FeatureView: View {
@StateObject var store: StoreOf<Feature>
init() {
_store = StateObject(
// This expression is only evaluated the first time the parent view is computed.
wrappedValue: Store(initialState: Feature.State()) {
Feature()
}
)
}
var body: some View { /* ... */ }
}
Important: The store's
ObservableObjectconformance does not have any impact on the actual observability of the store. You should continue to rely on theObservableState()macro for observation.