Maciej Swic
Maciej Swic

Reputation: 11359

Nested list doesn't update item labels when items change

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.

My Category

extension Category {
    @NSManaged public var name: String?
    @NSManaged public var products: NSSet?

    var sortedProducts: [Product] {
        return self.products.allObjects.sorted({ $0.name > $1.name })
    }
}

My List

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

Answers (1)

lorem ipsum
lorem ipsum

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

Related Questions