SwiftData error when trying to delete items from a list, Never access a full future backing data

When I try to delete items from my list stored with swift data, it instantly makes crash my app with this error message: SwiftData/BackingData.swift:482: Fatal error: Never access a full future backing data - PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(url: x-coredata://03DEFFA9-87EF-4E13-9448-946D9EBC17B6/Exercise/p8), implementation: SwiftData.PersistentIdentifierImplementation) with Optional(0252D555-649A-45B2-954C-7DD62A6DBAE4)

import SwiftUI
import SwiftData

struct WorkoutsView: View {
    @Environment(\.modelContext) var modelContext
    @Query(sort: [
        SortDescriptor(\Workout.name),
        SortDescriptor(\Workout.difficulty),
        SortDescriptor(\Workout.duration)
    ]) var workouts: [Workout]
    
    @State private var isEditing = false
    @State private var showingAddScreen = false
    
    var body: some View {
        NavigationStack {
            List {
                ForEach(workouts) { workout in
                    //design purpose code
                }
                .onDelete(perform: deleteWorkouts)
            }
            .navigationTitle("Workouts")
            .toolbar {
                ToolbarItem(placement: .topBarLeading) {
                    EditButton()
                }
                
                ToolbarItem(placement: .topBarTrailing) {
                    Button(action: {
                        showingAddScreen = true
                    }) {
                        Image(systemName: "plus")
                    }
                }
            }
            .sheet(isPresented: $showingAddScreen) {
                AddWorkoutView()
            }
        }
    }
    
    func deleteWorkouts(at offsets: IndexSet) {
        for offset in offsets {
            let workout = workouts[offset]
            modelContext.delete(workout)
        }
    }
}

I am seeing a similar issue mentioned here: https://developer.apple.com/forums/thread/758893?answerId=794462022#794462022

Seems to be related to deleting Models that have relationships.

FB14041374

The error message seems to indicate that the backing data doesn't exist when you delete the object.

Did you save the model before deleting it? If not, please try to explicitly save the model context when adding a model by calling try? modelContext.save(). That should make sure that the backing data of the model exists.

If that doesn't change anything, please share a minima project that contains only the code relevant to the issue, with detailed steps to reproduce the issue. I'll be happy to take a closer look.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

SwiftData error when trying to delete items from a list, Never access a full future backing data
 
 
Q