swiftui-state
State and data flow in SwiftUI apps: Observation (@Observable, @State, @Bindable, @Environment), legacy ObservableObject (@StateObject, @ObservedObject, @EnvironmentObject, @Published) in existing cod
Files1
View on GitHubSKILL.mdplugins/apple-plugin/skills/swiftui-state/SKILL.md
GitHubSwiftUI State and Data Flow
Detect the pattern first
| Signal | Pattern | Follow it |
|---|---|---|
@Observable classes, @State var model = …, @Bindable |
Observation (iOS 17 / macOS 14 / watchOS 10+) | default for new code |
ObservableObject, @Published, @StateObject |
Combine-era observation | keep it in that feature; do not mix both in one model |
import ComposableArchitecture, @Reducer, Store |
TCA | new features as reducers, dependencies via @Dependency |
*ViewModel classes per screen |
MVVM | one view model per screen, same naming |
Never introduce a second pattern. Migrating a feature from ObservableObject to @Observable is a separate change unless the BA spec asks for it.
Ownership
- Value state local to a view (a toggle, text field contents, a sheet flag):
@State private var. - A model the view creates and owns:
@State private var model = OrdersModel()with Observation (@StateObjectin legacy code). Never@ObservedObject/ plainletfor something the view creates — it is recreated on every parent render. - A model passed in by a parent: a plain
let/varproperty (Observation tracks reads automatically);@Bindable var modelwhen the view needs bindings ($model.title). - Shared across a subtree: inject with
.environment(model), read with@Environment(OrdersModel.self). - Bindings to parent-owned values:
@Binding. - User preferences that are not secret:
@AppStorage/@SceneStorage(scene restoration). Never tokens or personal data.
Observable models
@MainActor
@Observable
final class OrdersModel {
private(set) var state: LoadState<[Order]> = .idle
var filter: OrderFilter = .open
@ObservationIgnored private let client: any OrdersClient
init(client: any OrdersClient) {
self.client = client
}
func load() async {
state = .loading
do {
state = .loaded(try await client.orders(filter: filter))
} catch is CancellationError {
return
} catch {
state = .failed(error)
}
}
}
- Models that drive UI are
@MainActor(implicit when the target usesMainActordefault isolation). Do networking and heavy work throughasyncservices; assign results on the main actor. - One enum for screen state instead of
isLoading+error+itemscombinations. private(set)for state the view only reads; expose intent methods (load(),toggleFavorite(_:)) instead of letting views mutate internals.@ObservationIgnoredfor dependencies and caches that must not trigger updates.- Keep models free of SwiftUI types where possible — they are the unit under test.
Dependency injection
- Services (API clients, stores, clocks, analytics) are protocols with a live implementation, passed through initializers or the environment (
@Entry), so tests and previews can substitute them. - TCA projects:
@Dependency(\.ordersClient)withliveValue/testValue/previewValue. - No global singletons for new services unless the project's established pattern is a shared container — then register in it.
SwiftData
@Model
final class Draft {
var title: String
var createdAt: Date
@Relationship(deleteRule: .cascade) var attachments: [Attachment] = []
init(title: String, createdAt: Date = .now) {
self.title = title
self.createdAt = createdAt
}
}
- One
ModelContainerfor the app (.modelContainer(...)on the scene); views read with@Query(sort: \Draft.createdAt, order: .reverse)and filter with#Predicate; writes through@Environment(\.modelContext)or a model actor. - Background imports in a
@ModelActor actorwith its own context — never pass@Modelinstances across actors; passPersistentIdentifiers. - Schema changes: add a new
VersionedSchemaand aSchemaMigrationPlanstage when the app has shipped with the old schema; never rename or retype a stored property without one. - CloudKit-backed containers need optional or defaulted properties and no unique constraints — check before adding fields.
Core Data (existing apps)
- Use the existing
NSPersistentContainer/ stack; UI reads onviewContext, writes inperformBackgroundTaskor a background context;@FetchRequest/@SectionedFetchRequestin SwiftUI. - Model changes create a new model version (
.xcdatamodeld) with lightweight migration where possible; never edit the shipped version in place.
Async streams and system events
- Observe
AsyncSequences (NotificationCenter.default.notifications(named:), Core Location updates, SwiftData changes) inside.taskso they are cancelled with the view, or in the model with a storedTaskcancelled indeinit. - Combine pipelines only where the feature already uses Combine.