Reputation: 11359
I have a nested list of Categories
and their Products
in SwiftUI. If i tap a Product
, edit its name and go back to the list, the name doesn't update. The list of products comes from a computed property on the @FetchRequest
, and not directly from a @FetchRequest
and i have a feeling im missing a wrapper or something somewhere?
If i add a new Product, then the list updates, because that notifies the @FetchRequest
that something changes, I'm missing this "magic" for my computed property.
extension Category {
@NSManaged public var name: String?
@NSManaged public var products: NSSet?
var sortedProducts: [Product] {
return self.products.allObjects.sorted({ $0.name > $1.name })
}
}
List {
ForEach(categories) { category in
Section(header: Text(category.name)) {
ForEach(category.sortedProducts, id: \.self) { product in
NavigationLink(
destination: ProductFormView(edit: product),
label: {
Text(product.name)
})
}
}
}
}
Upvotes: 0
Views: 155
Reputation: 29613
Make a LabelView(product: product)
that has @ObservedObject var product: Product
That way you can observe the changes. Assuming Product
is a CoreData object or an ObsevervableObject
Upvotes: 1