In SwiftUI in iOS 18.1, `SectionedFetchRequest` is not refreshed when changes are done to the fetched entity's attributes.

Hi,

This issue started with iOS 18, in iOS 17 it worked correctly. I think there was a change in SectionedFetchRequest so maybe I missed it but it did work in iOS 17.

I have a List that uses SectionedFetchRequest to show entries from CoreData. The setup is like this:

struct ManageBooksView: View {
    @SectionedFetchRequest<Int16, MyBooks>(
        sectionIdentifier: \.groupType,
        sortDescriptors: [SortDescriptor(\.groupType), SortDescriptor(\.name)]
    )
    private var books: SectionedFetchResults<Int16, MyBooks>

    var body: some View {
        NavigationStack {
            List {
                ForEach(books) { section in
                    Section(header: Text(section.id)) {
                        ForEach(section) { book in
                            NavigationLink {
                                EditView(book: book)
                            } label: {
                                Text(book.name)
                            }
                        }
                    }
                }
            }
            .listStyle(.insetGrouped)
        }
    }
}
struct EditView: View {
       private var book: MyBooks
    init(book: MyBooks) {
       print("Init hit")
       self.book = book
    }
}

Test 1: So now when I change name of the Book entity inside the EditView and do save on the view context and go back, the custom EditView is correctly hit again.

Test 2: If I do the same changes on a different attribute of the Book entity the custom init of EditView is not hit and it is stuck with the initial result from SectionedFetchResults.

I also noticed that if I remove SortDescriptor(\.name) from the sortDescriptors and do Test 1, it not longer works even for name, so it looks like the only "observed" change is on the attributes inside sortDescriptors.

Any suggestions will be helpful, thank you.

While waiting for some official statement why this is happening in iOS 18 and not iOS 17, I found a workaround to force a refresh of the managedObjectContext when changes are saved.

Code sample:

@Environment(\.managedObjectContext) private var managedObjectContext

var body: some View {
    NavigationStack {
        Text("")
    }
    .onReceive(NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave)) { _ in
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            withAnimation {
                managedObjectContext.refreshAllObjects()
            }
        }
    }
}

It's not perfect but it gets the job done to update the list when any attributes are changed, not only those used as SortDescriptor. Hope this helps anyone facing this problem.

In SwiftUI in iOS 18.1, `SectionedFetchRequest` is not refreshed when changes are done to the fetched entity's attributes.
 
 
Q