I believe that the use of @AppStorage or @SceneStorage is causing view equality to fail in a way that @State does not. This is unexpected and can lead to much unneeded computation and wasted cycles.
Comments?
import SwiftUI
struct InnerView: View {
// Using @State, the toggle starts at false whenever you start the app.
// Using @AppStorage can let the app remember what the value of the toggle was.
// But if we comment out the @State version and uncomment the @AppStorage
// version, now we are hit by excessive recomputation.
@State var initialValue = false
// If you comment out the above @State variable and uncomment the @AppStorage
// version below, although the starting toggle state is preserved, we have a serious issue:
// the function someText() is called while dragging the slider in ContentView.
// I believe that view equality on InnerView is failing due to the AppStorage
// property wrapper in a way it does not for the State property wrapper.
//@AppStorage("initialValue") var initialValue = false
func someText() -> String {
print("someText called: \(Date())")
return "Blah"
}
var body: some View {
VStack {
Toggle("Toggle: ", isOn: $initialValue)
Text(someText())
}
}
}
struct ContentView: View {
@State var sliderValue = 0.0
var body: some View {
VStack {
Slider(value: $sliderValue)
InnerView()
}.padding()
}
}