I defined a many-to-many relationship between a set of exercises and workouts that you can create (crash occurs when generating a workout)
@Model
class Exercise: Identifiable, Equatable {
var id: UUID
var name: String
var imageName: String
var difficulty: Int
var guidelines: String
var muscle: String
// Define the relationship back to Workout
var workout: [Workout]?
init(name: String, imageName: String, difficulty: Int, guidelines: String, muscle: String, workout: [Workout]? = nil) {
self.id = UUID()
self.name = name
self.imageName = imageName
self.difficulty = difficulty
self.guidelines = guidelines
self.muscle = muscle
self.workout = workout
}
}
@Model
class Workout: Identifiable {
var id: UUID
var date: Date
var name: String
var difficulty: Int
var workingTime: Int
var recoveryTime: Int
var count: Int
var duration: Int
var isSet: Bool
var setNumber: Int?
var restTime: Int?
var animate: Bool = false
var streak: Int? = nil
// Define a one-to-many relationship with Exercise
@Relationship(deleteRule: .cascade, inverse: \Exercise.workout) var exercises: [Exercise]? = [Exercise]()
init(name: String, difficulty: Int, workingTime: Int, recoveryTime: Int, isSet: Bool, duration: Int, setNumber: Int? = nil, restTime: Int? = nil) {
self.id = UUID()
self.date = Date()
self.name = name
self.difficulty = difficulty
self.count = 0
self.duration = duration
self.workingTime = workingTime
self.recoveryTime = recoveryTime
self.isSet = isSet
self.setNumber = setNumber
self.restTime = restTime
}
}
the crash with the same error occurs at more than one place, but for example when I try to display pre-made workouts, called challenges, when I debug line by line, it load the first one but crash on the second one (there is 12 in total). on iOS 18, no problem tho.
it crashes in this part of the code exactly, taking a static list of workout and a static list of exercises and linking them together:
struct Challenge {
var challenges: [Workout]
init(challenges: [Workout], challengesExercises: [[Exercise]]) {
var counter: Int = 0
for challenge in challenges {
challenge.exercises = challengesExercises[counter]
counter += 1
}
self.challenges = challenges
}
}