Repository navigation
UIState and ManagedState have the same interface. Both types support Combine, ObservableObject, and the Observation framework. Both types protect the value with a lock.
Access to a property of the value:
state.titleis the value of the property. With the Observation framework, the read tracks the whole value.$state[\.title]is the value of the property. The read tracks onlytitle. A nested key path, such as$state[\.trip.title], is one field.$state.titleis a SwiftUIBinding<String>. A read through the binding tracks onlytitle.$state.ui.titleis aUIBinding<String>. The new propertyuireturns the same binding as$state().
If the type of a field is not Equatable, a read of the field tracks the whole value.
Observation:
- An observer that reads one field gets a notification only when that field gets a different value.
- An observer that reads
wrappedValuegets a notification for each change. - The observers get the notification before the value changes, as with the
@Observablemacro.
Locks:
- The new type
StateLockreplacesManagedStateLock. The new variableStateDefaultLockreplacesManagedStateDefaultLock. The old names are deprecated. UIStatehas the new initializerinit(wrappedValue:lock:)and the new methodwithLock(_:). If the initializer gets no lock, the state usesStateDefaultLock. The default value is.synced.UIStateandManagedStatenotify the subscribers and observers while they hold the lock. If a subscriber reads the state and the state uses a non-recursive custom lock, a deadlock occurs.
Changes that need an update of your code:
$state.titleno longer returns aUIBinding. Use$state.ui.title.$state.titleno longer returns the value of the property. Use$state[\.title].$state.title = valueno longer compiles. Writestate.title = value, or write$state.withLock { $0.title = value }.objectWillChangeofUIStateemits before each change. It does not emit when a subscriber subscribes.UIState.publisheris a constant. Code that assigns a value to it no longer compiles.- CombineExt imports SwiftUI.
Fixes:
- ARC releases a
UIStateinstance when no code holds it. Before this release, the current value of the publisher kept a strong reference to the instance. - The first access to
UIState.publisherfrom more than one thread is safe.