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 ...