I have some items and show an item count on the view. I have a toggle that reduces what is shown in the count/item list. The item count doesn't update when I use the toggle. What am I missing here? Cut down / mock example for conciseness.
struct SampleView: View {
@State var items:[Item]
@State var easyMode:Bool = false
@State var filtered:[Item] = []
init(...) {
// Fill items and filtered array
}
var body: some View {
Toggle(isOn: $easyMode, label:{
Text("Easy mode")
})
.onChange(of: self.easyMode, perform: { value in
filtered = filterItems()
// ^^ This is running but not
// updating stuff
})
// This text field does not update when filter changes
Text(String(filtered.count) + " items")
}
}
I tried having the filterItems generate a text string in i.e. @State var countLabel:String
and update the text, but there is a background thread issue. So I assume there is some weirdness where the filterItems isn't doing anything because its on a separate thread. What am I missing? Thanks!!