Observe RealmSwift.List of Embedded Objects

We seem to be having an issue observing an embedded List of Realm objects with Key Path observing. I thought this was possible starting with 10.14?

New in version 10.14.0.

You can observe a partially type-erased PartialKeyPath on Objects or RealmCollections.

and in that section of the documentation, there’s and example of observing a List via keypaths.

Here’s our setup:

class PersonClass: Object {
    @Persisted var embeddedCatList = RealmSwift.List<CatClassEmbedded>()
}

class CatClassEmbedded: EmbeddedObject {
    @Persisted var catName = ""
    @Persisted var aProperty = ""
    @Persisted var anotherProperty = ""
}

Person Jay has an embeddedCatList with 5 cats and we want to observe if any properties of any cat in the List changes. In the below code we get jay and add an observer to the PersonClass.embeddedCatList using Key Path

at the top level of the viewController we define the strong reference notification

var embeddedCatChangeNotification: NotificationToken?

then in a function we do this

let jay = realm.objects(PersonClass.self).where { $0.name == "Jay"}.first!

self.embeddedCatChangeNotification = jay.observe(keyPaths: [\PersonClass.embeddedCatList], { change in
    print("embedded cat list change")
})

We are not receiving any notifications when the embeddedCatList changes.

Interestingly, if we don’t use Key Path observations and observe the property directly, it does work

self.embeddedCatChangeNotification = jay.embeddedCatList.observe { [weak self] changes in ...

Maybe we’ve overlooked something?

Edit: Added a bug report

1 Like