Updating SwiftUI Views when changing a property of a Core Data object

I have a SwiftUI App project using Core Data. If I add a new object to the database, SwiftUI updates without problem.

If I edit an item in the database, the views do not update until they're reloaded. Is there a way to force SwiftUI to update when a property changes, rather than just when an object is added or removed from the database?

I have a list showing data from each object in the database, and I originally solved the updating problem by making the object passed to the view an @ObservedObject. This resulted in a crash when the row was deleted, as SwiftUI seemed to be trying to re-render the view before it animated out.

Replies

hi,

first a question: you're using @FetchRequest to drive your list, yes? your objects are Identifiable, yes? a little code would be helpful in diagnosing the issue.

and second, yeah, the ObservedObject thing will die if you delete the item it references. SwiftUI is still holding on to a reference to it because it's an ObservedObject. (i've been there!)

you might be better off without @ObservedObject, and sometimes it simply depends on how you write the List code as to whether its view gets updated correctly. but @FetchRequest certainly does pick up property changes.

looking forward to seeing more from you!

hope that helps,
DMG