CoreData not updating Property

I have created a project with CoreData having One to Many Relationship (1 Category to many Items). I am having problems updating the Item's 'done' property in Item's View it's updating only when i go back to Category View and then navigate back to Item View
Code Block
var body: some View {
      List {
        ForEach(category!.CitemsArray) { item in
              HStack {
                  Text("\(item.title!)")
                  .onTapGesture {
                            item.done.toggle()
                           do {
                               try moc.save()
                            } catch {
             print("error saving done value:\(error)")
                            }
                        }
                                Spacer()
              if item.done == true {
         Image(systemName: "checkmark.circle.fill")
                                } else {
          Image(systemName: "circlebadge")
                                }
                          }
                    }
                .onDelete(perform: deleteItems)
            }
            .sheet(isPresented: $isPresented, content: {
  ItemEditView(category: category, item: ItemModel())
            })
            .navigationBarItems(trailing: Button(action: {
                isPresented.toggle()
            }, label: {
                Image(systemName: "plus")
            }))
            .navigationBarTitle(category!.name!)
    }

I think i am doing something wrong in the .onTapGesture

Thanks in Advance
Accepted Answer
hi,

we do not see the definition of category in this view, but i am hoping that it would ideally be
Code Block
@ObservedObject var category: Category // not an optional, so reference category.CitemsArray in the ForEach

you should know that changing a property of one of the category's associated items is not seen as a change to the category object itself, and it will not trigger a visual update of the view.

when you do item.done.toggle() in the .onTapGesture, consider registering an explicit change on the category object with
Code Block
category.objectWillChange.send()


hope that helps,
DMG


CoreData not updating Property
 
 
Q