SwiftData: Unable to delete (Xcode 16)

I am using Xcode 16 and am doing a future app with SwiftData.

I have the data in a List. When I try to delete a row, it deletes and seems to save the deletion. But when I list the data again, what I deleted comes back.

Here's the code:

	{
			for index in indexSet {
				let chat = chatLog[index]
				modelContext.delete(chat)
					
//	do {
//	try modelContext.save()
//	
//	} catch {
//	
//			print("Error saving: \(error)")
//	
//	}


			}
	
	}

CHAT: Is a variable to hold the data.
CHATLOG: The variable in the List.


Thank you.
Dan Uff
Answered by Tooflexdev in 791995022

I have the same issue, on iOS 18 and MacOS 15 I can't delete an item. My item has relationships. It's working on iOS 17.5.

Also, I have the same issue when modifying an sub-item in my item.

Here are the logs:

Thread 0 name: Thread 0 Crashed: 0 libswiftCore.dylib 0x00000001946291a8 _assertionFailure(_:_:file:line:flags:) + 264 (AssertCommon.swift:147) 1 SwiftData 0x00000002524066a4 _FullFutureBackingData.getValue<A, B>(forKey:) + 780 (BackingData.swift:494) 2 SwiftData 0x0000000252406cb4 protocol witness for BackingData.getValue<A, B>(forKey:) in conformance _FullFutureBackingData<A> + 24 (<compiler-generated>:0) 3 SwiftData 0x00000002523a579c onOptionalArray #1 <A><A1>(_:) in PersistentModel.remove<A>(_:for:on:) + 316 (PersistentModel.swift:414) 4 SwiftData 0x00000002523a7340 specialized PersistentModel.withRelationship<A, B>(from:to:by:onModel:onOptionalModel:onArray:onOptionalArray:onCodableArray:onOptionalCodableArray:) + 1060 (PersistentModel.swift:0) 5 SwiftData 0x00000002523a82d0 specialized PersistentModel.remove<A>(_:for:on:) + 892 (PersistentModel.swift:421) 6 SwiftData 0x00000002523a62b0 onOptionalModel #1 <A><A1>(_:) in PersistentModel.addToInverse<A>(of:on:) + 524 (PersistentModel.swift:468) 7 SwiftData 0x00000002523a66b4 guardAndInvoke #1 <A><A1, B1><A2>(_:with:) in PersistentModel.withInverseRelationship<A, B>(of:from:to:onModel:onOptionalModel:onArray:onOptionalArray:onCodableArray:onOptionalCodableArray:) + 60 (PersistentModel.swift:521) 8 SwiftData 0x00000002523a68fc closure #2 in PersistentModel.withInverseRelationship<A, B>(of:from:to:onModel:onOptionalModel:onArray:onOptionalArray:onCodableArray:onOptionalCodableArray:) + 180 9 SwiftData 0x00000002523a7a94 partial apply for closure #3 in PersistentModel.withInverseRelationship<A, B>(of:from:to:onModel:onOptionalModel:onArray:onOptionalArray:onCodableArray:onOptionalCodableArray:) + 28 10 SwiftData 0x00000002523a70a4 specialized PersistentModel.withRelationship<A, B>(from:to:by:onModel:onOptionalModel:onArray:onOptionalArray:onCodableArray:onOptionalCodableArray:) + 392 (PersistentModel.swift:559) 11 SwiftData 0x000000025239fdfc PersistentModel.withInverseRelationship<A, B>(of:from:to:onModel:onOptionalModel:onArray:onOptionalArray:onCodableArray:onOptionalCodableArray:) + 776 (PersistentModel.swift:524) 12 SwiftData 0x00000002523a1f90 PersistentModel.addToInverse<A>(of:on:) + 1252 (PersistentModel.swift:487) 13 SwiftData 0x00000002523a3128 closure #3 in PersistentModel.setValue<A, B>(forKey:to:) + 816 (PersistentModel.swift:237) 14 SwiftData 0x00000002523ff5bc RelationshipCollection.enumerateModels(_:) + 392 (BackingData.swift:50) 15 SwiftData 0x000000025239f8c8 PersistentModel.setValue<A, B>(forKey:to:) + 1516 (PersistentModel.swift:234) 16 WriCo 0x0000000100a55754 closure #1 in SchemaV1.StoryScene.tags.setter + 136 17 WriCo 0x0000000100a54d5c closure #1 in SchemaV1.StoryScene.tags.setter + 36 18 WriCo 0x0000000100a815b8 partial apply for closure #1 in SchemaV1.StoryScene.tags.setter + 16 (<compiler-generated>:0) 19 libswiftObservation.dylib 0x000000026b469e2c ObservationRegistrar.withMutation<A, B, C>(of:keyPath:_:) + 100 (ObservationRegistrar.swift:364) 20 WriCo 0x0000000100a7f098 SchemaV1.StoryScene.tags.setter + 120 (@__swiftmacro_5WriCo8SchemaV1O10StorySceneC4tags18_PersistedPropertyfMa_.swift:12)

Let me reduce what you wrote down to the relevant bits:

{
	for index in indexSet {
		let chat = chatLog[index]
		modelContext.delete(chat)
	}
}

You haven't told us what is in chatLog, so we don't know whether you actually remove the deleted item from the array, or whether you repopulate chatLog after deleting the item from modelContext.

Can't really help you unless we know exactly what you're doing.

If chatLog reflects what's in modelContext, then if it shows the deleted item then that item definitely wasn't removed from modelContext.

Here's the entire code:

import SwiftData

struct ChatHistory: View {

	@Environment(\.modelContext) var modelContext
	
	// Sort SwiftData list by DATE:
	@Query(sort: \SwiftDatabase.date, order: .reverse, animation: .spring) private var chatLog: [SwiftDatabase]
	
	@Environment(\.dismiss) var dismiss
	@State private var actTxt: String = ""
	@State private var btnDel = false

    var body: some View {
    
					PullDownToDismiss()
    
			   NavigationStack {
						
						List {
								
								ForEach(chatLog) { show in
									
										HStack {
											
											// Show Date of Chat:
											Text("\(show.date.formatted(date: .numeric, time: .omitted))")

											.foregroundStyle(Color.init(uiColor: .secondaryLabel))
												.font(.system(size: 18, weight: .regular))
											
											Text("\(show.actTxt ?? "----")")
											.foregroundStyle(Color.init(uiColor: .label))
											.font(.system(size: 22, weight: .bold))
											
									.navigationTitle("Chat Log")
									.navigationBarTitleDisplayMode(.inline)
									
										.swipeActions(allowsFullSwipe: true) {
            Button("Delete", systemImage: "trash", role: .destructive) {
          
          
            }
									
													
						}
								
    }
									}.onDelete(perform: deleteLog)
										
    
    }
    
    }
    }
    
	public func deleteLog(_ indexSet: IndexSet)
	{
			for index in indexSet {
				let chat = chatLog[index]
				modelContext.delete(chat)
					
//	do {
//	try modelContext.save()
//	
//	} catch {
//	
//			print("Error saving: \(error)")
//	
//	}


			}
	
	}
													
							}
Accepted Answer

I have the same issue, on iOS 18 and MacOS 15 I can't delete an item. My item has relationships. It's working on iOS 17.5.

Also, I have the same issue when modifying an sub-item in my item.

Here are the logs:

Thread 0 name: Thread 0 Crashed: 0 libswiftCore.dylib 0x00000001946291a8 _assertionFailure(_:_:file:line:flags:) + 264 (AssertCommon.swift:147) 1 SwiftData 0x00000002524066a4 _FullFutureBackingData.getValue<A, B>(forKey:) + 780 (BackingData.swift:494) 2 SwiftData 0x0000000252406cb4 protocol witness for BackingData.getValue<A, B>(forKey:) in conformance _FullFutureBackingData<A> + 24 (<compiler-generated>:0) 3 SwiftData 0x00000002523a579c onOptionalArray #1 <A><A1>(_:) in PersistentModel.remove<A>(_:for:on:) + 316 (PersistentModel.swift:414) 4 SwiftData 0x00000002523a7340 specialized PersistentModel.withRelationship<A, B>(from:to:by:onModel:onOptionalModel:onArray:onOptionalArray:onCodableArray:onOptionalCodableArray:) + 1060 (PersistentModel.swift:0) 5 SwiftData 0x00000002523a82d0 specialized PersistentModel.remove<A>(_:for:on:) + 892 (PersistentModel.swift:421) 6 SwiftData 0x00000002523a62b0 onOptionalModel #1 <A><A1>(_:) in PersistentModel.addToInverse<A>(of:on:) + 524 (PersistentModel.swift:468) 7 SwiftData 0x00000002523a66b4 guardAndInvoke #1 <A><A1, B1><A2>(_:with:) in PersistentModel.withInverseRelationship<A, B>(of:from:to:onModel:onOptionalModel:onArray:onOptionalArray:onCodableArray:onOptionalCodableArray:) + 60 (PersistentModel.swift:521) 8 SwiftData 0x00000002523a68fc closure #2 in PersistentModel.withInverseRelationship<A, B>(of:from:to:onModel:onOptionalModel:onArray:onOptionalArray:onCodableArray:onOptionalCodableArray:) + 180 9 SwiftData 0x00000002523a7a94 partial apply for closure #3 in PersistentModel.withInverseRelationship<A, B>(of:from:to:onModel:onOptionalModel:onArray:onOptionalArray:onCodableArray:onOptionalCodableArray:) + 28 10 SwiftData 0x00000002523a70a4 specialized PersistentModel.withRelationship<A, B>(from:to:by:onModel:onOptionalModel:onArray:onOptionalArray:onCodableArray:onOptionalCodableArray:) + 392 (PersistentModel.swift:559) 11 SwiftData 0x000000025239fdfc PersistentModel.withInverseRelationship<A, B>(of:from:to:onModel:onOptionalModel:onArray:onOptionalArray:onCodableArray:onOptionalCodableArray:) + 776 (PersistentModel.swift:524) 12 SwiftData 0x00000002523a1f90 PersistentModel.addToInverse<A>(of:on:) + 1252 (PersistentModel.swift:487) 13 SwiftData 0x00000002523a3128 closure #3 in PersistentModel.setValue<A, B>(forKey:to:) + 816 (PersistentModel.swift:237) 14 SwiftData 0x00000002523ff5bc RelationshipCollection.enumerateModels(_:) + 392 (BackingData.swift:50) 15 SwiftData 0x000000025239f8c8 PersistentModel.setValue<A, B>(forKey:to:) + 1516 (PersistentModel.swift:234) 16 WriCo 0x0000000100a55754 closure #1 in SchemaV1.StoryScene.tags.setter + 136 17 WriCo 0x0000000100a54d5c closure #1 in SchemaV1.StoryScene.tags.setter + 36 18 WriCo 0x0000000100a815b8 partial apply for closure #1 in SchemaV1.StoryScene.tags.setter + 16 (<compiler-generated>:0) 19 libswiftObservation.dylib 0x000000026b469e2c ObservationRegistrar.withMutation<A, B, C>(of:keyPath:_:) + 100 (ObservationRegistrar.swift:364) 20 WriCo 0x0000000100a7f098 SchemaV1.StoryScene.tags.setter + 120 (@__swiftmacro_5WriCo8SchemaV1O10StorySceneC4tags18_PersistedPropertyfMa_.swift:12)

Glad to see I'm not the only one. Thank you!

SwiftData: Unable to delete (Xcode 16)
 
 
Q