Swift PartialKeyPath for observers not supported

class Dog {
    var name: String
    var age: Int
    var toys: List<Toy>
}

class Toy {
    var brand: String
    var name: String
}

If I’d like to observe Realm changes by observing Dogs Realm Results, filtering by using Keypaths I am not allowed to do:

let keyPath: PartialKeyPath<Dog> = [
        \Dog.toys.brand
]

(I’d like to get observers when any toy’s brand name for a dog changes)
Realm documentation seems wrong, because ‘toys’ which is a List does not have a ‘brand’.

  • What is described there is not possible

What specifically do you feel is not possible?

Objects in List can certainly be observed for changes - the documentation you linked is a tad out of date but it’s also covered in the SDK Docs

That being said, in the link you provided to that Realm article, I don’t believe this code is valid as I think the partial key path only works on single objects, not on a collection, results in this case.

let results = realm.objects(Company.self)
let notificationToken = results.observe(keyPaths: [\Company.orders.status]) { changes in
	// update UI
}

which means that [\Company.orders.status] should be ["orders.status"] if orders is a List Property

It’s possible I have the syntax wrong as well so probably need another set of eyes on this.

What I mean is that we are not allowed to use Swift’s PartialKeyPath to create an observer if we want to observe specific property on a child.

If we use String then it works, but I find PartialKeyPaths more developer friendly

In RealmSwift code, in observe method documentation it claims that it is possible to do what I proposed /.Dog.toys.brand

But Swifts compiler would not allow it. I am thinking of there is an another solution to handle this

Understood.

As mentioned, you can observe a property on a child of a single object.

let notificationToken = someDog.observe(keyPaths: [\Dog.name]) { changes in
	// update UI
}

but using PartialKeyPaths on a List appears to be broken? Not working as intended? Error in documentation?

We’ll have to see if a Realmer puts their eyes on this thread - if not, a bug report may be in order.