I am quite confused about how lists are saved in a realm.
If I have the following schema:
class First: Object {
@Persisted var children = List<Second>()
convenience init(children: [Second]) {
self.init()
self.children.append(objectsIn: children)
}
}
class Second: Object {
@Persisted var children = List<Third>()
convenience init(children: [Third]) {
self.init()
self.children.append(objectsIn: children)
}
}
class Third: Object {
}
If I try to save a First object like this:
let first = First(children: [Second(children : [Third()])])
let realm = try! Realm()
realm.write {
realm.add(first, update: .modified)
}
The children Second objects are not saved. What I have to do is this:
let first = First(children: [Second(children: [Third()])])
let realm = try! Realm()
let children = first.children
try! realm.write {
realm.add(children, update: .modified)
realm.add(first, update:.modified)
first.children.append(objectsIn: children)
}
`
and what is even more odd is that the children of the Second objects are save to the realm successfully.
Am I missing something here, or am I doing something wrong?