根据v0.102.0 的 Realm 文档,这是创建反向关系的方式:
人
class Person: Object {
// ... other property declarations
let dogs = List<Dog>()
}
狗 (v1)
class Dog: Object {
// ... other property declarations
let owners = LinkingObjects(fromType: Person.self, property: "dogs")
}
假设我们有另一个类,称为DogFood
,并且我们想要创建一个反向关系,称为buyers
that 跟踪哪些实例Person
具有Dog
吃 的实例DogFood
。我们可以尝试以下方法:
狗 (v2)
class Dog: Object {
// ... other property declarations
let favoriteFoods = List<DogFood>
let owners = LinkingObjects(fromType: Person.self, property: "dogs")
}
狗粮
class DogFood: Object {
// ... other property declarations
let buyers = LinkingObjects(fromType: Person.self, property: "dogs.favoriteFoods")
}
但是,这会引发以下错误:Property 'dogs.favoriteFoods' declared as origin of linking objects property 'buyers' does not exist.
有没有其他方法可以达到同样的效果?