9.12. SwiftUI Integration¶
Wire a ComponentVMOf<Model> to a SwiftUI view with a small Combine adapter
that bridges VMx change notifications into SwiftUI's ObservableObject
machinery. The adapter decides who owns the VM's lifetime; appearing and
disappearing never end it.
9.12.1. Reactivity Primitive¶
SwiftUI re-renders when an ObservableObject publishes through its
objectWillChange publisher. VMx VMs publish PropertyChangedMessage
to their hub and emit a string-keyed propertyChanged Combine
publisher. The adapter forwards every relevant event into
objectWillChange.send().
9.12.2. Mapping¶
| SwiftUI | VMx |
|---|---|
@StateObject adapter |
ObservableObject wrapper around a ComponentVMOf<M> |
objectWillChange.send() |
propertyChanged subscription |
Button(action: …) |
the domain command's execute(), for example a save command |
.disabled(…) on Save |
the command's canExecuteChanged, mirrored into a @Published flag |
.onAppear { … } |
construct an owned VM once; repeated appearances are no-ops |
.onDisappear { … } |
nothing terminal: the view may appear again |
| adapter released by SwiftUI | dispose an owned VM exactly once; never dispose a borrowed VM |
Disposal is terminal: a disposed VM cannot be constructed again, so tying
dispose() to .onDisappear breaks any view that reappears (tabs, navigation
stacks, lazy lists). Choose an ownership policy instead:
- Owned: the view created the VM. The adapter constructs it on first
appearance and disposes it once when SwiftUI releases the
@StateObject. - Borrowed: a parent VM or app state owns the VM, as the
Notes Workspace (
examples/swift/notes-showcase/) keeps itsWorkspaceVMfor the window's lifetime. The view only observes it.
9.12.3. Adapter And View¶
This is the exact code compiled and exercised by
langs/swift/Tests/VMxTests/SwiftUIRecipeTests.swift: appear, disappear, and
appear again; owned versus borrowed cleanup; Save and its enabled state; and a
surfaced construction failure. CI runs them on macos-15 runners with the default
Xcode and, on pushes, Xcode 16.0.
import Combine
import SwiftUI
import VMx
struct Note: Equatable {
var title: String
}
/// Who ends the view model's lifetime.
enum ViewModelOwnership {
/// The view created the VM: construct it on first appearance and dispose
/// it exactly once, when SwiftUI releases the adapter.
case owned
/// A parent owns the VM: the view observes it and never changes its
/// lifecycle.
case borrowed
}
/// Bridges a VMx view model into SwiftUI. Appearance never ends the VM's
/// lifetime, because a view that disappears may appear again.
final class ViewModelAdapter<Model>: ObservableObject {
let vm: ComponentVMOf<Model>
let ownership: ViewModelOwnership
/// A lifecycle failure for the host to show; never swallowed.
@Published private(set) var lifecycleError: (any Error)?
private var cancellables: Set<AnyCancellable> = []
init(_ vm: ComponentVMOf<Model>, ownership: ViewModelOwnership) {
self.vm = vm
self.ownership = ownership
vm.propertyChanged
.receive(on: RunLoop.main)
.sink { [weak self] _ in self?.objectWillChange.send() }
.store(in: &cancellables)
}
/// Call from `.onAppear`. Constructing an already constructed VM is a
/// no-op, so repeated appearances are safe.
func appeared() {
guard ownership == .owned, lifecycleError == nil else { return }
do {
try vm.construct()
} catch {
lifecycleError = error
}
}
deinit {
cancellables.removeAll()
if ownership == .owned {
vm.dispose()
}
}
}
/// Mirrors a command's `canExecute()` into a published flag. The flag is
/// updated on each `canExecuteChanged` signal, so a button's enabled state
/// follows the command's predicate.
final class CommandState: ObservableObject {
@Published private(set) var canExecute: Bool
private var cancellable: AnyCancellable?
init(_ command: any Command) {
canExecute = command.canExecute()
cancellable = command.canExecuteChanged
.receive(on: RunLoop.main)
.sink { [weak self] in self?.canExecute = command.canExecute() }
}
}
struct NoteView: View {
@StateObject private var adapter: ViewModelAdapter<Note>
@StateObject private var commandState: CommandState
private let save: any Command
/// `save` is the domain's save command, e.g. from a form or workspace VM.
init(vm: ComponentVMOf<Note>, ownership: ViewModelOwnership, save: any Command) {
_adapter = StateObject(wrappedValue: ViewModelAdapter(vm, ownership: ownership))
_commandState = StateObject(wrappedValue: CommandState(save))
self.save = save
}
var body: some View {
VStack {
if let error = adapter.lifecycleError {
Text("Could not open this note: \(String(describing: error))")
}
Text(adapter.vm.model.title)
Button("Save") { save.execute() }
.disabled(!commandState.canExecute)
}
.onAppear { adapter.appeared() }
}
}
The Save button runs the command it is given, typically the save or approve
command of a form or workspace VM. A component's selectCommand only selects it
in its parent; it persists nothing.
The button's enabled state comes from CommandState, which follows the
command's canExecuteChanged signal. Reading canExecute() in body alone does
not refresh the button when only the predicate changes.
Construction errors are not discarded with try?: the adapter stores them in
lifecycleError so the host can show them, and later cleanup cannot mask them.
9.12.4. Lifecycle Is Throwing (ADR-0053)¶
The Swift lifecycle operations construct(), destruct(), and reconstruct()
are throws (ADR-0053, superseding ADR-0037 §2.5). They match the catchable
exceptions of the C#, Python, and TypeScript flavors. An illegal transition,
such as construct() on a disposed VM, throws a catchable
StatusTransitionError. A concurrent call while a transition is in flight
throws the same error. The legal idempotent no-ops (construct from
Constructed, destruct from Destructed) return without throwing.
do {
try vm.construct()
} catch let error as StatusTransitionError {
// Recover — the VM is left in its prior settled state, not crashed.
print("illegal lifecycle transition: \(error)")
}
A non-child current assignment likewise has a throwing companion
(setCurrent(_:) throws, throwing CompositeMembershipError); see ADR-0053 §2.2.
It also throws for a child while disposal of the composite is pending, and for
any value inside the caller's own uncommitted membership transaction (ADR-0173).
9.12.5. Fuller Example¶
The SwiftUI Notes Workspace flagship lives at
examples/swift/notes-showcase/. Its
NotesShowcaseCore target keeps the pure VM layer separate from SwiftUI, while
the app target contains the Combine-to-SwiftUI binding bridge.
9.12.6. Cross-Flavor Parity¶
Each recipe bridges VM change notifications into the host's re-render
primitive. This adapter forwards the VM-local propertyChanged stream into
objectWillChange. The Avalonia (avalonia.md) and Textual
(textual.md) recipes also forward VM-local notifications. React
uses the packaged @thekaveh/vmx-react adapter (react.md).