CoreData SwiftUI - List not updating

Hi,

I'm creating a To-do list app with SwiftUI. There's a main 'List' view with all the lists users create. When you tap on a list, it takes you to the Detail View with the items in the list. The problem is, when I make a change to the list in the Detail View, it doesn't change in the main list.

The changes are indeed getting saved, as if I restart the app, the changes are persistent.

I've looked at similar issues but none of the solutions seem to be working.

Code Block swift
struct HomeView: View {
    @Environment(\.managedObjectContext) var viewContext
    
    @FetchRequest(entity: ItemGroup().entity, sortDescriptors: [NSSortDescriptor(keyPath: \ItemGroup.createdAt, ascending: false)])
    var groups: FetchedResults<ItemGroup>
        
    var body: some View {
        NavigationView {
            List {
                Section
{
                    ForEach(groups, id: \.self) { group in
                        NavigationLink(destination: DetailView(group: .constant(group)), label: {
                                GroupRow(group: group)
                            })
// ...


Code Block swift
struct GroupRow: View {
    @ObservedObject var group: ItemGroup
    var body: some View {
        VStack(alignment: .leading) {
            ProgressView(value: group.progress)
            Text(group.wrappedName)
            Text("\(group.ongoingItems.count) Ongoing")
        }
    }
}


Code Block swift
struct DetailView: View {
    @Environment(\.managedObjectContext) private var viewContext
    
    @FetchRequest(entity: Item().entity, sortDescriptors: [NSSortDescriptor(keyPath: \Item.createdAt, ascending: false)])
    private var items: FetchedResults<Item>
    
    @Binding var group: ItemGroup
    
    
    var body: some View {
...


Please suggest what might be the solution! Thanks!
Answered by rahulon12 in 673603022
Update: fixed.

I changed the 'group' in DetailView from a Binding to an ObservedObject and added group.objectWillChange.send() in the necessary places.
Accepted Answer
Update: fixed.

I changed the 'group' in DetailView from a Binding to an ObservedObject and added group.objectWillChange.send() in the necessary places.
I wouldn't use @ObservedObject for passing variables of type ItemGroup entity especially since all of the data is stored in the Core data model. Just pass around the data through view hierarchy with @Binding or no property wrapper. Honestly haven't seen a difference in performance when I did that in my app, but it makes more sense not to use ObservedObject property wrapper when you are not accessing an observable object class.

Hope that makes sense.
CoreData SwiftUI - List not updating
 
 
Q