Hello everyone,
i'm working on an app to make my family life easier. it's was working fine with simple features but now I have added some feature but it's not working on the simulator or on my iPhone. The live preview is working fine...
error message is Thread 1: Fatal error: Could not create ModelContainer: SwiftDataError(_error: SwiftData.SwiftDataError._Error.loadIssueModelContainer)
nothing is very clear in the message except:
Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={entity=Recette, attribute=isApero, reason=Validation error missing attribute values on mandatory destination attribute}}}
issue looks to come from my variable "var isApero: Bool". Unfortunately I can't see the problem.
below my model:
import SwiftData
@Model
final class Recette {
// @Attribute(.unique)
var creationDate: Date
var name: String
var type: String
var ingredient1: String
var ingredient2: String
var imageName: String
var people1: Bool
var people2: Bool
var people3: Bool
var people4: Bool
var isEntree: Bool
var isPlat: Bool
var isDessert: Bool
var isApero: Bool
var isClassify:Bool
init(name: String, type: String, ingredient1: String, ingredient2: String, imageName: String, people1: Bool = false, people2: Bool = false, people3: Bool = false, people4: Bool = false, isEntree: Bool = false, isPlat: Bool = false, isDessert: Bool = false, isApero1: Bool = false, isClassify:Bool = true)
{
self.creationDate = Date()
self.name = name
self.type = type
self.ingredient1 = ingredient1
self.ingredient2 = ingredient2
self.imageName = imageName
self.people1 = people1
self.people2 = people2
self.people3 = people3
self.people4 = people4
self.isEntree = isEntree
self.isPlat = isPlat
self.isDessert = isDessert
self.isApero = isApero1
self.isClassify = isClassify
}
}
here my detail view for items:
import SwiftUI
import SwiftData
struct DetailRecette: View {
@Bindable var recette: Recette
var body: some View {
VStack {
TextField("nouvelle recette", text: $recette.name)
.textFieldStyle(.roundedBorder)
TextField("Ingredient1", text: $recette.ingredient1)
.textFieldStyle(.roundedBorder)
TextField("Ingredient2", text: $recette.ingredient2)
.textFieldStyle(.roundedBorder)
.padding(.bottom, 24.0)
HStack {
Text("people1")
Toggle(recette.people1 ? "" : "", isOn: $recette.people1)
}
HStack
{
Text("people2")
Toggle(recette.people2 ? "" : "", isOn: $recette.people2)
}
HStack {
Text("People3")
Toggle(recette.people3 ? "" : "", isOn: $recette.people3)
}
HStack {
Text("People4")
Toggle(recette.people4 ? "" : "", isOn: $recette.people4)
}
Divider()
.padding(.bottom, 50)
HStack {
Text("Entrée")
Toggle(recette.isEntree ? "" : "", isOn: $recette.isEntree)
.padding(.trailing, 50)
.onChange(of:recette.isEntree) {
if recette.isEntree == true {
recette.isPlat = false
recette.isDessert = false
recette.isClassify = false
recette.isApero = false
}}
Text("Plat")
Toggle(recette.isPlat ? "" : "", isOn: $recette.isPlat)
.padding(.trailing, 50)
.onChange(of:recette.isPlat) {
if recette.isPlat == true {
recette.isEntree = false
recette.isDessert = false
recette.isClassify = false
recette.isApero = false
}}
}
HStack {
Text("Dessert")
Toggle(recette.isDessert ? "" : "", isOn: $recette.isDessert)
.padding(.trailing, 47)
.onChange(of:recette.isDessert) {
if recette.isDessert == true {
recette.isEntree = false
recette.isPlat = false
recette.isClassify = false
recette.isApero = false
}}
Text("Apéro")
Toggle(recette.isApero ? "" : "", isOn: $recette.isApero)
.padding(.trailing, 50)
.onChange(of:recette.isApero) {
if recette.isApero == true {
recette.isEntree = false
recette.isPlat = false
recette.isDessert = false
recette.isClassify = false
}
}
}
}
Divider()
HStack {
Text("A classer")
.padding(.leading, 120.0)
// .frame(alignment: .trailing)
Toggle(recette.isClassify ? "" : "", isOn: $recette.isClassify)
.padding(.trailing, 140.0)
.onChange(of:recette.isClassify) {
if recette.isClassify == true {
recette.isEntree = false
recette.isPlat = false
recette.isDessert = false
recette.isApero = false
}}
}
}
}
#Preview {
DetailRecette(recette: Recette(name: "Recette", type: "Type", ingredient1: "ingredient1", ingredient2: "ingredient2", imageName: "image"))
}
Do you have any idea about whey this happens?
Thank you for your help
Julien.