Most frequent occurrence of Realm

I have an object with a column supervisor

. There supervisor

are the following values ​​in:

  • John
  • Mary
  • Phil
  • John

How can I get John back?

class Person: Object {
    dynamic var name: String = String()

      

+3


source to share


3 answers


Realm Results

implement RealmCollectionType

RealmCollectionType

has property count

and aggregate operations max(_:)

.
What you need to do is something like (the following pseudocode won't compile if you just specify a dot):



results.filter {element in element.count == results.max (byCount)}. first

Any operation on an object Result

is performed in Realm database

, not in the device's memory.

+4


source


You can get all the objects Person

associated with your column supervisor

(a List<T>

?) And count name

between those objects Person

, where the natural maximum follows.

var frequencies = [String: Int]()
for person in yourRealmObject.supervisor {
    frequencies[person.name] = (frequencies[person.name] ?? 0) + 1
}

if let mostCommonName = frequencies.max(by: { $0.value < $1.value })?.key {
    // ... mostCommonName holds string "John" (if that is most common)
}

      

Naturally, more than one name may be the most common in your column, in which case you may need to return an array from the most common

if let mostCommonNames = frequencies.values.max()
    .map({ maxCount in frequencies.filter { $0.value == maxCount }.map { $0.key } }) {
    // ... mostCommonNames [String]
}

      

Or, if you really want to count the occurrences of the same object (instead of looking at a property name

: for example, there may be several different Person

: s s name

"John"

), you might consider making your object Person

match Hashable

and count the frequencies of real unique objects ( rather than frequencies of different values ​​of name

object properties ).



// Person is Hashable ...
frequencies = [Person: Int]()
for person in yourRealmObject.supervisor {
    frequencies[person] = (frequencies[person] ?? 0) + 1
}

// ...

      

Since it Person

is a reference type, the key

one corresponding to the most common object will be a reference to that object (for example "John"

Person

).


However, I may not follow your question: please expand it if this is not what you need.

+1


source


You have Person

and want to get one of your Person

supervisors objects matching the given name:

class Person: Object {
  dynamic var name: String = String()
  let supervisors = List<Person>()

  convenience init(name: String) {
    self.init()
    self.name = name
  }
}

let realm = try! Realm()

var me: Person!

try! realm.write {
  me = Person(name: "Me")
  for supervisor in ["John", "Mary", "Phil"] {
    me.supervisors.append(Person(name: supervisor))
  }
  realm.add(me)
}

let john = me.supervisors.filter("name == 'John'")[0]

print(john)

      

Prints:

Person {
  name = John;
  supervisors = RLMArray <0x6080000f2280> (

  );
}

      

0


source







All Articles