I'm using Realm Swift for the following code:
class Item: Object {
@Persisted(primaryKey: true) var name = ""
@Persisted(originProperty: "items") var collection: LinkingObjects<SomeCollection>
}
class SomeCollection: Object {
@Persisted(primaryKey: true) var name = ""
let items = List<Item>()
}
On app startup when I initialize the Realm
instance I got:
Property 'SomeCollection.items' declared as origin of linking objects property 'Item.collection' does not exist" UserInfo={NSLocalizedDescription=Schema validation failed due to the following errors:
Interestingly, if I change the line for LinkingObjects
to:
let collection = LinkingObjects(fromType: SomeCollection.self, property: "items")
it then works fine, however, after adding an Item
instance to the items
list of SomeCollection
, the collection
property of Item
instance doesn't show any linked SomeCollection
, which I thought should be linked automatically?
What am I doing wrong here?
Thanks for help!
CodePudding user response:
This
let items = List<Item>()
Needs to be this
@Persisted var items = List<Item>()
or
@Persisted var items: List<Item>
depending on the use case.
The let
syntax is how we used to do it.
That's called and Inverse Relationship and is covered in the documentation a bit further.