Search code examples
swiftrealmpredicate

Realm - how to query for a List greater than 0 objects


I want all results of Persons that have at least one cat. I tried this:

    let results = realm.objects(Person.self).filter("cats.count > 0")

But I get this error:

Terminating app due to uncaught exception 'Invalid property name', reason: 'Property 'count' not found in object of type 'Cat''

The person object looks like this:

class Person: Object {
    @objc dynamic var name = ""
    let cats = List<Cat>()
}

What is the proper predicate for this filter?


Solution

  • I was so close!

        let results = realm.objects(Person.self).filter("cats.@count > 0")
    

    (needed the "@" before count)