简体   繁体   中英

How to perform Realm count query

How can I do a count query on Realm?

for example this is my model

class Dog: Object {
  dynamic var name = ""
}
class Person: Object {
  dynamic var name = ""

  let dogs = List<Dog>()
}

I want to fetch all persons with at least one dog something like

Realm().objects(Person).filter("dogs.@count > 0")

but @count isn't supported as i understand

Yes, Realm still does not support .@count query. You can work around that you modify the Person model to have a count property. Then you update the count property when you append a dog object to the dogs array.

This feature request is tracked by Issue #1166 https://github.com/realm/realm-cocoa/issues/1166

Update

Realm supports @count and other collection queries from v0.96.

ResultsList的新聚合表达式( @count@min@max@sum@avg )的语法如下所示:

realm.objects(Person.self).filter("dogs.@count > 0")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM