Thank you that worked. I hadn't thought about creating a Query in the initializer. Thanks for the suggestions also, I didn't find the SwiftData tag because it was not visible in the list of matches in the tag search, the others were all matches to WWDC2023 sessions.
Post
Replies
Boosts
Views
Activity
Using NavigationStack, however, did not eliminate the issue. Thank you for your recommendations, I think the second point may well lead to a solution since the stack trace suggest the failure occurs when attempting to access the id property which is not Optional @NSManaged public var id: UUID.
Many thanks for your comments, @DelawareMathGuy. It will take me some time to absorb them and experiment with different approaches. I have tried changing the target to iOS 16 and using the new NavigationStack approach which effectively defers the creation of the CourseDetailView until the navigation link is activated (thank heavens!!) and therefore rules out the possibility of a reference to a Course being held by its ViewModel if the detail view hasn't been used before deletion.
@DelawareMathGuy, looking closer at the ForEach loop in my code I can certainly see a potential problem in the way the NavigationLink creates the CourseEditView with a strong reference to the later deleted course and would explain why my simplified test code above works OK. Not sure how to refactor this but is this a case which might be solved by the new NavigationStack view in iOS16?
@DelawareMathGuy, unfortunately it's not on GitHub and, at this stage I don't want to make the whole app public. I have posted the code for CourseEditView and CourseEditViewModel in a new post below. The entity Course only has 2 properties (id: UUID and name:String) both are set on creation in view model's saveChanges() function which is called whenever the name property changes and the isValid property of the view model is true.