@Relationship crash on ios17.5 but not on ios18

In my app, I worked with ios18 by default and I had no issue, everything was working fine. However, when I wanted to test it with an iOS 17.5 simulator or real device, it is unusable because it crash when object with a relationship are created.

on the first line you can see my relationship, and under it it is the extended relationship macro unmodifiable.

has someone already encountered this? any clue of a hide modification of relationship working behavior in ios18?

this is really annoying cause it make me unable to reduce the minimum deployment target of my app to ios17

If you can share your code that triggered the exception, I may be able to take a look and comment.

iOS 18 fixed some SwiftData bugs, but without looking into the relevant code, folks probably can't tell if the issue you described is indeed a bug and if there is a workaround for it (on iOS 17).

Maybe worth mentioning, when working with SwiftData relationships, a rule of thumb is to insert the model objects to the model context before related them, as discussed in SwiftData inverse relationship not updating. Failing to do so may trigger an exception, which is pretty similar to what you showed, when the relationship is accessed.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

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
    }
}

The use of relationships, as shown in your code snippet, looks pretty normal. It is a surprise to me that the code triggers a crash on iOS 17. It can be a bug that occurs on iOS 17 and has been fixed on iOS 18, but if you target to support iOS 17 and need to get to the bottom, please start with filing feedback report with a minimal project that reproduces the issue and share your report ID, I'd take a closer look from there.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

@Lxdro @DTS Engineer

Did you get to the bottom of this issue? I am experiencing the exact same thing and would like to know if there's a work around.

@Relationship crash on ios17.5 but not on ios18
 
 
Q