I'd like to implement Realm inverse-relationships support for creation with a dictionary.
But the assertion fails: the dog from the dictionary was not created.
import RealmSwift
class Dog: Object {
dynamic var name: String?
dynamic var owner: Person?
}
class Person: Object {
dynamic var name: String?
let dogs = LinkingObjects(fromType: Dog.self, property: "owner")
}
func sample() -> Person? {
// Get the default Realm
let realm = try? Realm()
let sampleValue: [String: Any] = ["name": "Harry", "dogs": [["name": "Belle"]]]
var person: Person? = nil
try? realm?.write {
person = realm?.create(Person.self, value: sampleValue, update: false)
}
assert(person?.dogs.isEmpty == false)
return person
}
Note: RealmSwift (2.1.2)
LinkingObjectsis a lookup mechanism, and not an actual representation of an on-disk store. As such, it's not possible to insert data into it via a write transaction.However, if you redesign your schema, so
Personhas aListofDogobjects, andDogitself defines aLinkingObjectsto determine its parents, then your code of inserting aPersonandDogin the same dictionary should work. :)