I have two one-to-many models and I'm getting an unexplained crash when trying to access the relationship properties of the models.
The reason for the error is as follows:
Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1038f4448)
Xcode shows that the error occurs in the model's .getValue(for: .rows) method.
This is my SwiftData model and other code:
@Model class Row {
var section: Section?
init(section: Section? = nil) {
self.section = section
}
init() {
self.section = nil
}
}
@Model class Section {
@Relationship(.cascade, inverse: \Row.section)
var rows: [Row] = []
init(rows: [Row]) {
self.rows = rows
}
}
class ViewController: UIViewController {
var container: ModelContainer?
override func viewDidLoad() {
super.viewDidLoad()
do {
container = try ModelContainer(for: [Section.self, Row.self])
} catch {
print(error)
}
let row = Row()
let section = Section(rows: [row])
section.rows.append(row)
var myRows = section.rows //Accessing relationship properties causes crash
print("hello") //no print
}
}
If I access the relationship property of the model, the program crashes. Such as passing to a variable.
var myRows = section.rows
Or just printing the relationship property of the model also crashes.
print(section.rows)
Xcode 15 beta 4 and 5.