I’m using the @Observable, @Bindable in SwiftUI. I have a simple ViewModel with two properties:
@Observable
final class AlarmLevelSetupViewModel {
var displayedValueLevel1: Decimal = 1
var displayedValueLevel2: Decimal = 2
}
And a view that renders two rows:
struct TestAlarms: View {
@Bindable var viewModel: AlarmLevelSetupViewModel
let values: [Decimal] = [0, 1, 2, 3, 4, 5]
var body: some View {
AlarmLevelRowView(
title: "level 2",
color: .accentRed,
radiationType: .radiationLevel(.countRate),
values: values,
selectedValue: $viewModel.displayedValueLevel2
)
AlarmLevelRowView(
title: "level 1",
color: .accentYellow,
radiationType: .radiationLevel(.countRate),
values: values,
selectedValue: $viewModel.displayedValueLevel1
)
}
}
When I change displayedValueLevel1, both AlarmLevelRowView instances get invalidated and re-rendered, even though the second row only uses displayedValueLevel2.
I expected only the view whose property changed to be redrawn.