Skip to main content

Store

The Store is the runtime that powers your TCA application. It holds the current state, processes actions through reducers, and executes effects. You typically create one store at the root of your application and pass scoped versions to child views.

The Store Type

Store.swift
Source: Store.swift:98-141

Creating a Store

Basic Initialization

Create a store with initial state and a reducer:
App.swift

With Dependencies

Override dependencies at store creation:

Accessing State

In SwiftUI Views

Access state directly from the store when using @ObservableState:
FeatureView.swift
With @ObservableState, you don’t need @ObservedObject or other property wrappers. The store integrates with Swift’s Observation framework.

Using withState

For non-observable contexts or when you need a snapshot:
Source: Store.swift:166-174
State accessed through withState is a snapshot. Changes to state won’t be observed over time.

Sending Actions

Send actions to the store to trigger state changes:
Source: Store.swift:176-192

With Animation

Source: Store.swift:194-204

With Transaction

Source: Store.swift:206-218

Scoping

The scope method transforms a store to work with a subset of state and actions:
Store.swift
Source: Store.swift:260-268

Basic Scoping

AppView.swift

Why Scoping?

Modularity

Views only access the state they need

Type Safety

Compiler ensures child views can’t access parent state

Performance

Views only re-render when their slice of state changes

Testability

Child features can be tested in isolation

Store Task

The StoreTask type represents the lifecycle of effects:
Store.swift
Source: Store.swift:457-481

Awaiting Effects

Tie effect lifecycle to SwiftUI’s .task modifier:
When the view disappears, the task is automatically cancelled, which cancels the effect.

Manual Cancellation

Store Publisher

For Combine-based observation:
Source: Store.swift:361-366
With @ObservableState, prefer using Swift’s Observation framework over Combine publishers.

StoreOf Type Alias

Use StoreOf for more concise type signatures:
Store.swift
Source: Store.swift:402

Observation

TCA integrates with Swift’s Observation framework:

iOS 17+

Automatic observation using Swift’s native Observable protocol:

iOS 13-16

Uses the Perception library for backward compatibility:
The @ObservableState macro handles both cases automatically. You don’t need to change your code.

Testing Stores

Use TestStore for testing:
FeatureTests.swift

Store Lifecycle

Initialization

  1. Store is created with initial state
  2. Dependencies are prepared
  3. Reducer is configured
  4. State observation is set up

Action Processing

  1. Action is sent via store.send()
  2. Reducer processes action, mutating state
  3. Effects are executed
  4. Observers are notified of state changes

Deinitialization

  1. All running effects are cancelled
  2. Child stores are deallocated
  3. Observations are cleaned up
Source: Core.swift:42-217

Best Practices

1

Create Store at App Root

Create a single store in your app’s entry point:
2

Scope to Child Views

Use scope to pass focused stores to children:
3

Avoid State Copies

Don’t copy state out of the store:
4

Use @ObservableState

Always apply @ObservableState to your state types:

Common Patterns

Optional Child Stores

Scope to optional child state:

Store in @StateObject

For creating stores in SwiftUI views:
Prefer passing stores from parent views rather than creating them in child views. This makes testing easier.

Multiple Store Instances

For previews or isolated features: