Problem
I was using swift data on my project in what I thought was a not very complex data model and then after I added the delete function it gave me this error and crashed the simulator every time.
DataModel
import Foundation
import SwiftData
@Model
class Deck {
var name: String
var cards: String
init(name: String, cards: String) {
self.name = name
self.cards = cards
}
}
And this is where I was fetching the data.
DeckView
import SwiftUI
import SwiftData
struct DeckView: View {
@State private var showingSheet = false
@Environment(\.modelContext) var modelContext
@Query var decks: [Deck]
var body: some View {
NavigationStack {
ZStack {
Color.background
.ignoresSafeArea()
List {
ForEach(decks) { deck in
HStack {
VStack(alignment: .leading) {
Text(deck.name)
Text(deck.cards)
}
Spacer()
Button {
// Action
} label: {
Image(systemName: "ellipsis.circle")
}
}
}
.onDelete(perform: deleteDeck)
}
.scrollContentBackground(.hidden)
}
.navigationTitle("Decks")
.navigationBarTitleDisplayMode(.automatic)
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Image(systemName: "plus")
.onTapGesture {
// showingSheet.toggle()
addSamples()
}
}
}
.sheet(isPresented: $showingSheet) {
NewDeckSheet()
}
}
}
func addSamples() {
let swiftd = Deck(name: "SwiftData", cards: "20")
let swifty = Deck(name: "Swift", cards: "21")
let swiftyui = Deck(name: "SwiftUI", cards: "32")
modelContext.insert(swiftd)
modelContext.insert(swifty)
modelContext.insert(swiftyui)
}
func deleteDeck(_ indexSet: IndexSet) {
for index in indexSet {
let deck = decks[index]
modelContext.delete(deck)
}
}
}
What the console returns
Deck = "(<NSEntityDescription: 0x600002c88790>) name Deck, managedObjectClassName NSManagedObject, renamingIdentifier Deck, isAbstract 0, superentity name (null), properties {\n cards = \"(<NSAttributeDescription: 0x6000035a9080>), name cards, isOptional 0, isTransient 0, entity Deck, renamingIdentifier cards, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 300 , attributeValueClassName NSNumber, defaultValue (null), preservesValueInHistoryOnDeletion NO, allowsCloudEncryption NO\";\n name = \"(<NSAttributeDescription: 0x6000035a9100>), name name, isOptional 0, isTransient 0, entity Deck, renamingIdentifier name, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null), preservesValueInHistoryOnDeletion NO, allowsCloudEncryption NO\";\n}, subentities {\n}, userInfo {\n}, versionHashModifier (null), uniquenessConstraints (\n)";
}, fetch request templates {
}, destinationModel=(<NSManagedObjectModel: 0x6000038a69e0>) isEditable 0, entities {
Deck = "(<NSEntityDescription: 0x600002c84420>) name Deck, managedObjectClassName Deck, renamingIdentifier Deck, isAbstract 0, superentity name (null), properties {\n cards = \"(<NSAttributeDescription: 0x60000359f680>), name cards, isOptional 0, isTransient 0, entity Deck, renamingIdentifier cards, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null), preservesValueInHistoryOnDeletion NO, allowsCloudEncryption NO\";\n name = \"(<NSAttributeDescription: 0x60000359f580>), name name, isOptional 0, isTransient 0, entity Deck, renamingIdentifier name, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null), preservesValueInHistoryOnDeletion NO, allowsCloudEncryption NO\";\n}, subentities {\n}, userInfo {\n}, versionHashModifier (null), uniquenessConstraints (\n)";
}, fetch request templates {
}, reason=Can't find or automatically infer mapping model for migration, NSUnderlyingError=0x60000156cb70 {Error Domain=NSCocoaErrorDomain Code=134190 "(null)" UserInfo={entity=Deck, property=cards, reason=Source and destination attribute types are incompatible}}}
And then: SwiftData/ModelContainer.swift:144: Fatal error: failed to find a currently active container for Deck
And: Failed to find any currently loaded container for Deck)
Any help would be appreciated, thank you!