I have a Thing Core Data entity with a name field. When I display a [Thing] in a List, I want to be able to edit the name inside the child views. Here's the code that does achieve this, but it feels like poor architecture. A ThingView should really be passed thing, not the thing.name!
Instead, I really want to pass the Thing to ThingView, but if I just did that, then updating thing.name would not cause the view to update because Thing is a reference type. Any thoughts on clean design here?
struct ContentView: View {
@ObservedObject var observableAllData: ObservableAllData
var body: some View {
NavigationView{List(observableAllData.allData.things.indices) { index in
NavigationLink(destination:ThingView(
thingName: $observableAllData.allData.things[index].name)) {
Text(observableAllData.allData.things[index].name)}
}
}
}
}
struct ThingView: View {
@Binding var thingName: String
func updateThing() {
thingName = "NEW"
}
var body: some View {
VStack{
Text(thingName)
Button(action: updateThing) { Text("Update it!")}
}
}
}
Edit: Clarification that I want both the ThingView and the ContentView
to update when clicking on "Update it".