In some scenario you need to use WithPerceptionTracking inside the block. #21
-
Descriptionimport Perception
import SwiftUI
@Perceptible
class ColorModel {
var color: Color = .clear
}
struct ContentView: View {
@State var model = ColorModel()
var body: some View {
let _ = print("\(Self.self): tracked change.")
WithPerceptionTracking {
VStack {
ForEach(0 ... 3, id: \.self) { _ in
// WithPerceptionTracking {
model.color
// }
}
}
}
}
}
#Preview {
ContentView()
} If I comment out the above content, I will get the following runtime warning: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
In a similar scenario where you are using // This indicates that you should use `WithPerceptionTracking` inside the `GeometryReader` block.
GeometryReader { proxy in
WithPerceptionTracking {
// Your code here
}
} |
Beta Was this translation helpful? Give feedback.
-
@alfredcc This is to be expected. |
Beta Was this translation helpful? Give feedback.
@alfredcc This is to be expected.
WithPerceptionTracking
is a synchronous call, and so if you hit an escaping closure that is executed later, it must also be instrumented withWithPerceptionTracking
. If someone figures out a way around this we'd love to see it, but we don't think it's possible to do from outside SwiftUI.