Imagine you have a struct
like
struct TheObject: Identifiable, Hashable {
var id: String
var title: String
}
Now you store an array of that type inside a ViewModel
class Model: ObservableObject {
@Published var objects = [TheObject(id: "id", title: "title")]
}
You create a NavigationLink
for each item of objects
. Those NavigationLinks feature a button, which replaces the objects
array with the same object (by id) but a different property
NavigationStack {
ForEach(model.objects) { object in
NavigationLink("Text", value: object)
}
.navigationDestination(for: TheObject.self) { object in
Text(object.title)
.onTapGesture {
model.objects = [TheObject(id: "id", title: "title changed")]
}
}
}
What did I expect to happen?
I expected my opened NavigationLink to be updated with the new title.
What did I get instead?
The view did not change. Instead I have to pop the view and reopen it to reveal the changes.