I have the following view model:
class ViewModel: ObservableObject {
@Published var brightnessValue: Double = 0.0
@Published var saturationValue: Double = 0.0
@Published var contrastValue: Double = 0.0
}
In the UI layer I then have 3 sliders, implemented as SwiftUI views, each bound to a Double from the above view model. Whenever a slider changes a property, all 3 sliders redraw. That's because a change to a Published property in an ObservableObject redraws all the views that reference it.
This is how the SwiftUI views looks like:
struct RootView: View {
@ObservedObject var viewModel: ViewModel
var body: some View {
VStack {
AdjustmentSlider(name: "brightness", sliderValue: $viewModel.brightnessValue)
AdjustmentSlider(name: "saturation", sliderValue: $viewModel.saturationValue)
AdjustmentSlider(name: "contrast", sliderValue: $viewModel.contrastValue)
}
.background(.random)
}
}
struct AdjustmentSlider: View {
let name: String
@Binding var sliderValue: Double
var body: some View {
Slider(value: $sliderValue)
.background(.randomColor) // I use this to visualize the redrawing.
}
}
Finally, this is the overall (sample) app design:
This is will be part of a very complex app, with dozens of sliders, so I'm afraid this constant redrawing could become a non-trivial performance bottleneck as the view model becomes more complex.
Any suggestions on how to design a more efficient view model (i.e., less redraws) for SwiftUI? And, more broadly, is this even a valid concern when using SwiftUI circa iOS 16?

onEditingChanged(false). I don't think it would affect a performance, but it creates a better boundary between "internal state of the UI component" and a "value user selected", and less times model needs to change