Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

SwiftData data crashes with @Relationship

I've noticed that SwiftData's @Relationship seems to potentially cause application crashes. The crash error is shown in the image.

Since this crash appears to be random and I cannot reproduce it under specific circumstances, I can only temporarily highlight that this issue seems to exist.

@Model final class TrainInfo {
    @Relationship(deleteRule: .cascade, inverse: \StopStation.trainInfo)
    var stations: [StopStation]?
}

@Model final class StopStation {
    @Relationship
    var trainInfo: TrainInfo?
}

/// some View

    var origin: StopStationDisplayable? {
        if let train = train as? TrainInfo {
            return train.stations?.first(where: { $0.isOrigin }) ?? train.stations?.first(where: { $0.isStarting })
        }
        return nil
    }

    // Some other function or property
    func someFunction() {
        if let origin, let destination {
            // Function implementation
        }
    }

The stack trace seems to indicate that your code was trying to retrieve the value of an attribute, but the attribute didn't exist in the SwiftData model.

The code snippet shows that TrainInfo and StopStation is a to-many relationship, which has nothing wrong. isOrigin and isStarting seem to be an attribute of StopStation. They are not included in your code snippet, and so I can't comment.

Overall, the information you provided doesn't seem to be enough to diagnose the issue...

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

SwiftData data crashes with @Relationship
 
 
Q