I'm coming from a Ruby on Rails-like data structure in terms of relationships.
So in Rails: Foo has many Bars and Bar has one Foo.
Going through the RealmSwift docs, I came up with this, I think:
class Foo: Object {
// other props
var bars = List<Bar>() // I hope this is correct
}
class Bar: Object {
// other props
@objc dynamic var foo: Foo?
}
If the above is corrct, I struggle to know how to create this relationship object.
// I need to create Foo before any Bar/s
var foo = Foo()
foo.someProp = "Mike"
var bars = [Bar]()
var bar = Bar()
bar.someProp1 = "some value 1"
bars.insert(bar, at: <a-dynamic-int>)
This is where I am at a full stop:
// Create Foo
try! realm.write {
realm.add(foo)
// But.... I need to append bars, how?
}
try! realm.write {
for bar in bars {
// realm.add(bar)
// I need to: foo.append(bar) but how and where?
}
}
In the end, I should be able to foo.bars
to see an array of bars
and bar.foo
to get foo
foo
and bar
have not yet been created so do not know how to chain the lot to save at once. Possible? How? If you are providing an answer, can you post a reference to the doc for future reference? That would count as an answer for me. Thanks
This should get you started:
Elsewhere:
To get foo from bar:
If I'm understanding your comment correctly: