Skip to content
This repository has been archived by the owner on Jun 7, 2020. It is now read-only.

Commit

Permalink
Improve performance in RealmAssorter
Browse files Browse the repository at this point in the history
  • Loading branch information
cardoso committed Dec 4, 2018
1 parent 02e5d7f commit 3f98c51
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions Rocket.Chat/Helpers/RealmAssorter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ class RealmAssorter<Object: RealmSwift.Object & UnmanagedConvertible> {
var objects: Results<Object>

var section: ArraySection<String, Object.UnmanagedType> {
return ArraySection(model: name, elements: objects.compactMap { $0.unmanaged })

var elements = [Object.UnmanagedType]()
elements.reserveCapacity(objects.count)
for object in objects {
guard let unmanaged = object.unmanaged else {
continue
}

elements.append(unmanaged)
}

return ArraySection(model: name, elements: elements)
}
}

Expand Down Expand Up @@ -62,7 +73,11 @@ class RealmAssorter<Object: RealmSwift.Object & UnmanagedConvertible> {
self.model?.invalidate()
self.model = model.observe { _ in
let oldValue = self.sections
let newValue = self.results.map { $0.section }
var newValue = [ArraySection<String, Object.UnmanagedType>]()
newValue.reserveCapacity(self.results.count)
for result in self.results {
newValue.append(result.section)
}

let changes = StagedChangeset(source: oldValue, target: newValue)

Expand Down

0 comments on commit 3f98c51

Please sign in to comment.