I am using Realm and its REST API's mapping method to map JSON data to my models and their child relationships automatically. It works great, except when I add in a "To-One" relationship on the child models referencing the parent. The mapping goes all out of whack and creates additional entries that don't exist.
The parent model:
class Foo: Object {
dynamic var id = 0
dynamic var title = ""
let bars = List<Bar>() // Children
override class func primaryKey() -> String {
return "id"
}
}
The child model:
class Bar: Object {
dynamic var id = 0
dynamic var title = ""
dynamic var foo = Foo() // Reference to parent
override class func primaryKey() -> String {
return "id
}
}
Per the documentation I am mapping the JSON using the convenience method:
let foos = JSON["foos"] as! [NSDictionary]
let realm = Realm()
realm.write {
for foo in foos {
realm.create(Foo.self, value: foo, update: true)
}
}
I'm aware I could set up the mapping manually, but I want to make use of the convenience method if possible at all. Does anyone know how to set this up?
I think you better use
Inverse Relationships
for ToOne relationship instead.Foo
class will be the same. but you will changefoo
property inBar
class, like the following.