Guard NavigationLink destination inits against deleted @Models

A closure-based NavigationLink builds its destination view eagerly for
every row on every parent-list body evaluation — including the view-graph
update SwiftUI runs the instant SyncEngine.delete does context.delete +
save. Mapping the row's just-deleted @Model to a document there reads a
persisted property on a dead model, which traps in SwiftData
(_InitialBackingData.getValue -> assertionFailure). This is the "Crashed
when deleted a workout" TestFlight report on 2.3 (124).

Add WorkoutDocument(fromLive:) (nil when isDeleted) + a .deletedPlaceholder
and route every destination-init / updatedAt-absorb map through it:
phone WorkoutLogListView + SplitDetailView (read split.id) and watch
WorkoutLogListView. Fixes the whole crash class, not just the report.

Claude-Session: https://claude.ai/code/session_01BQcEWmAPA78338QuEwRkAh
This commit is contained in:
2026-07-10 10:47:10 -04:00
parent 8b20d7ad08
commit 85e15823a3
5 changed files with 49 additions and 6 deletions
@@ -28,7 +28,11 @@ struct WorkoutLogListView: View {
init(workout: Workout) {
self.workout = workout
_doc = State(initialValue: WorkoutDocument(from: workout))
// A closure-based `NavigationLink` builds this destination eagerly for every
// row in the parent list including during the update that fires when a phone
// push removes a workout so map only a live entity; a deleted one traps in
// SwiftData. The placeholder is never shown (the row is on its way out).
_doc = State(initialValue: WorkoutDocument(fromLive: workout) ?? .deletedPlaceholder)
}
/// The split this workout came from (read-only on the watch), used to offer
@@ -109,8 +113,10 @@ struct WorkoutLogListView: View {
// at. Clock skew across devices degrades this to a skipped absorb
// never to a clobber of local state.
.onChange(of: workout.updatedAt) { _, incoming in
if incoming > doc.updatedAt {
doc = WorkoutDocument(from: workout)
// ...but never absorb from an entity a push has just deleted: mapping a
// deleted @Model traps.
if incoming > doc.updatedAt, let fresh = WorkoutDocument(fromLive: workout) {
doc = fresh
}
}
.navigationDestination(item: $selectedLogID) { logID in