Open watt opened 1 month ago
Perceptible state was accessed but is not being tracked. Track changes to state by wrapping your view in a 'WithPerceptionTracking' view. This must also be done for any escaping, trailing closures, such as 'GeometryReader',
LazyVStack
(and all lazy views), navigation APIs ('sheet', 'popover', 'fullScreenCover', etc.), and others.
I am experiencing the same issue in the context of a TCA feature. The warning is raised by the selection binding of the Picker. When building with Xcode 15.4.0 there is no runtime waring, but when building with Xcode 16.0.0 there is a runtime warning. Wrapping the Picker in WithPerceptionTracking
does not solve the warning.
@Reducer
struct ContentFeature: Reducer {
@ObservableState
struct State: Equatable {
let indices = [0, 1, 2]
var index: Int = 0
}
enum Action {
case indexChanged(Int)
}
var body: some Reducer<State, Action> {
Reduce { state, action in
switch action {
case .indexChanged(let index):
state.index = index
return .none
}
}
}
}
struct ContentView: View {
@Perception.Bindable var store: StoreOf<ContentFeature>
var body: some View {
List {
Label("Hello World", systemImage: "globe")
WithPerceptionTracking {
Picker("Picker", selection: $store.index.sending(\.indexChanged)) {
WithPerceptionTracking {
ForEach(store.indices, id: \.self) { index in
Text("Number \(index)")
.tag(index)
}
}
}
}
}
}
}
PickerPerceptionTrackingSample.zip
Technical information
We are encountering the same issue. For now we managed to workaround the warning with a Picker wrapper type based on @KaiOelfke solution from a slack thread:
public struct _Picker<Label, SelectionValue, Content>: View
where Label: View, SelectionValue: Hashable, Content: View {
let label: Label
let content: Content
let selection: Binding<SelectionValue>
public init(
_ titleKey: LocalizedStringKey,
selection: Binding<SelectionValue>,
@ViewBuilder content: () -> Content
) where Label == Text {
self.label = Text(titleKey)
self.content = content()
self.selection = selection
}
public var body: some View {
_PerceptionLocals.$skipPerceptionChecking.withValue(true) {
Picker(selection: selection, content: { content }, label: { label })
}
}
}
Description
A SwiftUI
Picker
bound to a@Perceptible
model triggers the "Perceptible state was accessed but is not being tracked" runtime warnings in iOS 18.It seems to be due to access to the selection binding from the resolved body style. Example stack trace below.
Example stack trace
``` #0 0x0000000103593d9c in PerceptionRegistrar.perceptionCheck(fileID:filePath:line:column:) at /Users/awatt/Development/swift-perception/Sources/Perception/PerceptionRegistrar.swift:214 #1 0x0000000103593ac0 in PerceptionRegistrar.accessChecklist
@Observable
macro or another tool from theObservation
framework, please file it directly with Apple.main
branch of this package.Expected behavior
No warning for this case.
Actual behavior
Spurious runtime warnings.
Steps to reproduce
I was able to reproduce with this view dropped into the Perception example app. The warning is triggered on first render.
Perception version information
1.3.5
Destination operating system
iOS 18.0
Xcode version information
Version 16.0 (16A242d)
Swift Compiler version information