Inverse Relationship to Embedded Object

Is it “ok” to create an inverse relationship from an object back to an embedded object using the LinkingObject property type?

It works and we have not had any issues doing that but… does it work by design or is it a fluke that make not work in the future?

Let me provide an example. Starting with three objects, a Person, an EmbeddedDog and a City

class Person: Object {
    @Persisted var personName = ""
    @Persisted var embeddedDogList = RealmSwift.List<EmbeddedDog>()

class EmbeddedDog: EmbeddedObject {
    @Persisted var dogName = ""
    @Persisted var city: City!

class City: Object {
    @Persisted var cityName = ""
    @Persisted(originProperty: "city") var linkedDog: LinkingObjects<EmbeddedDog>

The person has an embeddedDog (List) and then the Dog has a forward reference to a City, and that Cty has in inverse relationship to the EmbeddedDog via LinkingObjects.

We write some test data

let p = Person()
p.personName = "Jay"

let d = EmbeddedDog()
d.dogName = "Scraps"

let c = City()
c.cityName = "Anytown" = c


try! realm.write {

and then we want to know which dog is in our city

let c = realm.objects(City.self).first!  
let d = c.linkedDog.first!

and the output is


so it works… is this a safe design?

Yes, this works as designed. We know that it may seem a bit awkward with how embedded objects are contained in other objects, but we felt it’s less awkward than the alternatives (e.g. the backlink pointing to the embedded object’s parent).

1 Like


It’s actually very well done and provides a lot of functionality to EmbeddedObjects.

Thanks for the verification.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.