Chasing down non-View Perception tracking warnings? #146
-
I notice in the PR for the Swift 6.2 back port (#141) the following note:
Does this only apply to I'm having trouble tracking down a non-View access as the backtrace disappears into AttributeGraph shenanigans that are non-local to my project. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Actually, let's make things easier - here's a tiny sample project that reproduces the issue I am having. I've deliberately made the violation a fatalError to make it more visible. I can't tell if this situation is a false positive, or if I have a real tracking violation on my hands? |
Beta Was this translation helpful? Give feedback.
-
This is an interesting one! What seems to be happening with This seems like a gotcha worth documenting, unless the developer experience could be improved somehow. When you write: let nameBinding: Binding<String> = $viewModel.stateCache["foo", \.name]
// equivalently:
let cacheBinding: Binding<StateCache> = $viewModel.stateCache
let nameBinding: Binding<String> = cacheBinding["foo", \.name] you're actually only accessing A workaround that you could use here (although I wish no one had to know about it!) is to call EditorView(
- name: $viewModel.stateCache["foo", \.name],
- value: $viewModel.stateCache["foo", \.value]
+ name: $viewModel[dynamicMember: \.stateCache["foo", \.name]],
+ value: $viewModel[dynamicMember: \.stateCache["foo", \.value]]
) |
Beta Was this translation helpful? Give feedback.
The issue here is nested observable objects. Perception instruments the projection of a
@Perception.Bindable
object, but you are accessing a property on a nested object. The fix is to localize the@Perception.Bindable
to that nested object before handing it off to SwiftUI:Nesting objects and having this kind of access is subtle, so if you have ideas to improve the output of the warning message, please let us know!