Replies: 3 comments
-
|
I'm curious if you had an attempt at fixing this or not? |
Beta Was this translation helpful? Give feedback.
-
|
@watt Thanks for the report! While we're definitely open to a fix here, the issue appears to be reproducible in vanilla SwiftUI with import SwiftUI
@main
struct ExampleApp: App {
let model = DemoModel()
var body: some Scene {
WindowGroup {
DemoView(model: model)
}
}
}
class DemoModel: ObservableObject {
@Published var toggle1 = false
@Published var toggle2 = false
}
struct DemoView: View {
@ObservedObject
var model: DemoModel
var body: some View {
VStack {
ToggleView(name: "1", isOn: $model.toggle1)
ToggleView(name: "2", isOn: $model.toggle2)
}
}
}
struct ToggleView: View {
var name: String
@Binding var isOn: Bool
var body: some View {
let _ = print("evaluated ToggleView \(name)")
Toggle("isOn: \(isOn)", isOn: $isOn)
}
}And we use There are 2 other ways I know of to derive bindings, and we've employed both in the past but they have other issues:
And so we finally landed on using I'm going to migrate this to a discussion since it's really an Apple bug, but we would certainly take a PR that fixes things if it doesn't regress anything else! |
Beta Was this translation helpful? Give feedback.
-
|
Looks like this is fixed in Perception 2.0. The toggle demo above only prints for the modified toggle. MacOS 15.5 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
It seems that a change to any binding derived from
@Perception.Bindablewill invalidate every binding that is derived from that same bindable.Looking at the source for
Bindable, it seems this is the reason:A
withPerceptionTrackingblock is run for eachkeyPaththat is subscripted, but theonChangeis not scoped in any way, so any change will invalidate the entireBindable.This is a regression from 1.1.4 to 1.1.5 and all later versions.
Checklist
@Observablemacro or another tool from theObservationframework, please file it directly with Apple.mainbranch of this package.Expected behavior
Changes to a single binding derived from a bindable only invalidate observations of that binding.
Actual behavior
Changes to a single binding invalidate every binding derived from that bindable.
Steps to reproduce
Run this view from the example app:
When run with Perception 1.1.4 or Apple Observation, changing a toggle causes a single log for the
ToggleViewthat is invalidated.Under Perception 1.1.5 and later, changing a toggle emits logs for both
ToggleViews.Perception version information
1.6.0
Destination operating system
iOS 16.4
Xcode version information
Version 16.1 (16B40)
Swift Compiler version information
Beta Was this translation helpful? Give feedback.
All reactions