Class Definition
Initialization
Initializes a store from an initial state and a reducer.Parameters:
initialState: The state to start the application inreducer: The reducer that powers the business logic of the applicationprepareDependencies: A closure that can be used to override dependencies that will be accessed by the reducer
Usage Example
You will typically construct a single store at the root of your application:Methods
Sending Actions
Sends an action to the store.This method returns a Returns: A
StoreTask, which represents the lifecycle of the effect started from sending an action. You can use this value to tie the effect’s lifecycle and cancellation to an asynchronous context, such as SwiftUI’s task view modifier:StoreTask that represents the lifecycle of the effect executed when sending the action.Sends an action to the store with a given animation.Parameters:
action: An actionanimation: An animation
Sends an action to the store with a given transaction.Parameters:
action: An actiontransaction: A transaction
Scoping
scope(state:action:)
<ChildState, ChildAction>(KeyPath<State, ChildState>, CaseKeyPath<Action, ChildAction>) -> Store<ChildState, ChildAction>
Scopes the store to one that exposes child state and actions.This can be useful for deriving new stores to hand to child views in an application:Scoping in this fashion allows you to better modularize your application.Parameters:
state: A key path fromStatetoChildStateaction: A case key path fromActiontoChildAction
State Access
Calls the given closure with a snapshot of the current state of the store.A lightweight way of accessing store state when state is not observable and
state property is unavailable.Parameters:body: A closure that takes the current state of the store as its sole argument. If the closure has a return value, that value is also used as the return value of thewithStatemethod.
body closure.Properties
A publisher that emits when state changes.This publisher supports dynamic member lookup so that you can pluck out a specific field in the state:
Scoping Example
For a tab view application with multiple tabs:Type Aliases
A convenience type alias for referring to a store of a given reducer’s domain.Instead of specifying two generics:You can specify a single generic:
Supporting Types
StoreTask
Store.send(_:) that represents the lifecycle of the effect started from sending an action.
Methods:
cancel(): Cancels the underlying taskfinish(): Waits for the task to finishisCancelled: A Boolean value that indicates whether the task should stop executing
StorePublisher
ObservableObject Conformance
The store conforms toObservableObject but is not observable via the @ObservedObject property wrapper. This conformance is completely inert and its sole purpose is to allow stores to be held in SwiftUI’s @StateObject property wrapper.
Instead, stores should be observed through Swift’s Observation framework (or the Perception package when targeting iOS <17) by applying the @ObservableState() macro to your feature’s state.