How to Perform Realm Count Query

How Query and Count entries in a Realm Database

They way you were computing the count required all Dog objects to be loaded into memory which is really inefficient. This is why you were seeing such poor performance. You want to take advantage of Realm's lazy loading features. You may want to read up on that.

I would update your Dog object by getting rid of your managed Person property and replace it with LinkingObjects. If you store a Dog in a Person.dogs List, then realm will create a back link to the Person for you. You will likely want to do the same thing with Cat. That way you can set up some really powerful nested queries.

For convenience you can add a computed Person property to index into the LinkingObjects. Just know that you won't be able to use that property in any of your queries.

class Dog: Object {
@objc dynamic var name = ""
let persons = LinkingObjects(fromType: Person.self, property: "dogs")
var person: Person? { persons.first }
}

One way to compute the count is to query of all Person objects and sum the count of dogs for each Person.

let count = realm.objects(Person.self).reduce(0) { result, person in
return result + person.dogs.count
}

Another options is to query for Dog objects that have a corresponding Person. If a Dog is not in a Person.dogs List, then it won't show up the query.

let realm = try! Realm()
let count = realm.objects(Dog.self).filter("persons.@count > 0").count

Either option is going to be much, much more efficient than what you had.

Hope this helps.

Realm Query to count number of unique entries

At a high level, if you have an array of Strings as shown in your question, and you want to get only unique Strings (no duplicates) you can cast it to a Set and you're done.

let nameArray = ["Adam", "Adam", "Ben", "Ben", "Ben", "Charlie", "Darren", "Darren"]
let nameSet = Set(nameArray)
print(nameSet)

and the output is

["Darren", "Charlie", "Ben", "Adam"]

If you would then like it ordered to be used as a dataSource, it can be cast back to an Array and sorted

let uniqueNameArray = Array(nameSet)
let orderedUniqueNameArray = uniqueNameArray.sorted()
print(orderedUniqueNameArray)

and the output

["Adam", "Ben", "Charlie", "Darren"]

(there are a lot of shortcuts that could be taken and you could probably make it a one-liner but I left it verbose for clarity)

EDIT

If you want to get Realm objects that are unique based on a certain property, you can use the .distinct function. Assume the Realm PersonClass as a name property

let people = realm.objects(PersonClass.self)
let uniquePeopleByName = people.distinct(by: ["name"])

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

I was so close!

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

(needed the "@" before count)



Related Topics



Leave a reply



Submit