Relationship filter


#1

I’m still working on my songwriting app. I’ve added another realm, Writers. Writers has a many to many relationship with Songs. I want to populate a tableview with a list of songs a particular writer has contributed to.

I’m crashing with ‘Invalid predicate’, reason: ‘Key paths that include an array property must use aggregate operations’" on the line

coWritenSongs = realm.objects(Song.self).filter(“writers.name = %@”, writerItem.name)

I know that aggregate functions are sum and so forth but I don’t understand why I am getting this error. Am I just going about getting the data I need in the wrong way?


#2

What you are trying to do is to get any songs that have a certain person as a writer. It’s likely your Song class has a writers List (array) so conceptually you need to let the filter know you are going to be looking through that List property for a matching writers name. Do that using ANY (meaning to query over key paths)

let songResults = realm.objects(Song.self).filter("ANY writers.name = %@", writerItem.name)

Docs are here but just has a mention of the ANY keyword and no other explanation Filtering


#3

Yeah those docs are lacking a bit. I read over them a couple times before posting and didn’t make the connection.

Thanks though, that did the trick.