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)
}
}
}