I'm lost and have been searching these forums (and Google) for hours.
I have a pretty straightforward Core Data class
with a custom extension for making the fetch simpler. (Thanks to Donny Wals!)
extension Item {
static var defaultFetchRequest: NSFetchRequest<Item> {
let request: NSFetchRequest<Item> = Item.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(keyPath: \Item.sort_title, ascending: true), NSSortDescriptor(key: "title", ascending: true)]
return request
}
}
That works perfectly fine (as far as I can tell) in a List View:
struct ItemsView: View {
@FetchRequest(fetchRequest: Item.defaultFetchRequest)
var items: FetchedResults<Item>
var body: some View {
NavigationView{
List(items, id: \.self){ item in
ZStack{
ItemCellView(item: item)
NavigationLink(destination: ItemDetailView(item: item)) {
EmptyView()
}
}
}
}
}
}
My Item Detail View works fine (again, as far as I can tell) and I know its self.moc.save()
does save because (a) when I return to the list and then return to the detail, the changed data is there and (b) when I close and reopen the app in Simulator, the primary list shows the data change.
The problem is that unless I quit and restart the Simulator, the main list does not refresh. When I save() in the detail view and return to the main list, the list doesn't display the changes. Again, though, I know the changes were made because they are there when I stop and restart the app in Simulator.
Do I need to do something to make the FetchedResults<item>
"listen" for changes? In UIKit I'd do a reload the table on viewDidAppear, but that seems contrary to SwiftUIness. Can anyone tell me what I'm missing here?
- Note that I've removed a bunch of extraneous code for styles, padding, etc.