This piece of code was working properly until I Introduced the Exercise Model. I have tried everything like using migration scheme, removing the app, cleaning build folder, trying on different devices and simulator. Nothing seems to work now. I can't get the app to launch.
struct LogbookApp: App {
@State private var sharedModelContainer: ModelContainer = {
let schema = Schema([
Workout.self,
Exercise.self
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return try ModelContainer(for: schema, configurations: [modelConfiguration])
} catch(let error) {
fatalError("Could not create ModelContainer: \(error)")
}
}()
var body: some Scene {
WindowGroup {
MainCoordinator()
.view()
}
.modelContainer(sharedModelContainer)
}
}
The models look like this
@Model
final class Exercise {
var name: String
var workout: Workout?
init(name: String, workout: Workout? = nil) {
self.name = name
self.workout = workout
}
}
@Model
final class Workout {
@Attribute(.unique) var id = UUID()
@Relationship(deleteRule: .cascade) var exercises: [Exercise]?
var type: WorkoutType
var date: Date
var duration: Int
var customDescription: String
init(id: UUID = UUID(),
type: WorkoutType,
date: Date,
duration: Int,
customDescription: String = "",
exercises: [Exercise]? = []) {
self.id = id
self.type = type
self.date = date
self.duration = duration
self.customDescription = customDescription
self.exercises = exercises
}
}