ios_catalogue1-5 Exercise 3: Testing the Delete Order ====================================================== There is no single correct answer to record: crashes of this kind depend on the iOS and Xcode version, and my sources did not agree on a fix. What to do, and how to read the results: Variants of deleteItem (always on a real item shown in DetailView, in the simulator): (a) delete first, then dismiss, no guard try? repo.delete(item); dismiss() The detail view is still on screen when the model disappears, so any re-render in between reads a deleted model. This is the shape of the crash reports ("backing data was detached" / "invalidated"). (b) dismiss, then delete in a Task, no guard dismiss(); Task { @MainActor in try? repo.delete(item) } The pop is started before the delete, so the view usually stops rendering first. Not guaranteed, because SwiftUI decides when to run the pop; a re-render that reads the item between the two would still be unsafe. (c) the chapter's version: (b) plus `if item.isDeleted { Color.clear } else { ... }` in body Even if a re-render happens, the body no longer reads the model once it is deleted. Record for each: crash or not; the exact console message (for example "This model instance was invalidated because its backing data could no longer be found in the store"); the iOS version and Xcode version. Run each a handful of times: timing-dependent failures can be intermittent, which is why "it worked once" proves little. Which to ship: (c), unless (c) misbehaves on your setup. It costs one conditional and one local variable, and it fails safe: the worst case is a blank frame while the screen is closing. If isDeleted does not exist or does not become true in time, find another reliable signal (for example item.modelContext == nil) and use that. Longer-term alternative that avoids the whole class of problem: navigate by identifier (item.persistentModelID) and have the detail screen FETCH the item with a predicate. A fetch that finds nothing returns nil, which you can turn into "This item no longer exists", exactly like the Android course's NotFound state. Note that modelContext.model(for:) is NOT a safe lookup: it can return an object even when the id no longer exists.