Post

Replies

Boosts

Views

Activity

Reply to SwiftData - Context missing for optional
Thank you so much for your suggested solutions @Nevs! The third option that check modelContext == nil seems to work for me on iOS 18. For example this works for me var body: some View { Text("The school has \(school.students.count) students") Button("Delete school", action: { context.delete(school) try! context.save() }) } @Model SchoolModel { @Relationship(deleteRule: .cascade, inverse: \StudentModel.school) var swiftDataStudents: [StudentModel]? = [] } var students: [StudentModel] { set { swiftDataStudents = newValue } get { if self.modelContext == nil { return [] } return swiftDataStudents ?? [] } }
1w