I have the following query on my Realm database
realm.objects(Event)
.filter("ANY presentation.speakers.lastName CONTAINS [c]%@", searchTerm)
Unfortunately it's not working, I'm getting the following error
'Invalid predicate', reason: 'Aggregate operations can only be used on RLMArray properties'
presentation is an optional entity defined like this on Event class
public dynamic var presentation : Presentation?
speakers is a List<PresentationSpeakers> defined like this on presentation
public let speakers = List<PresentationSpeaker>()
My feeling is that ANY must operate directly over a collection but speakers is not a direct property of Event.
What's wrong with my query and how can I implement it correctly?
Since presentation is a to-one relationship, there's no need to write ANY in your query:
realm.objects(Event)
.filter("presentation.speakers.lastName CONTAINS [c]%@", searchTerm)
The ANY is implicit for the speakers property because it is a nested keypath. To specify something other than ANY, you would use a modifier on speakers like this:
realm.objects(Event)
.filter("presentation.speakers[FIRST].lastName CONTAINS [c]%@", searchTerm)
However, Realm doesn't support this kind of query yet.
For more information on predicates and which ones are support by Realm, see Realm's predicate cheat sheet: https://realm.io/news/nspredicate-cheatsheet/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With