From 3f98c51c922bcf9a25b40d6c767ddddf550d9f06 Mon Sep 17 00:00:00 2001 From: Matheus Cardoso Date: Tue, 4 Dec 2018 17:46:48 -0200 Subject: [PATCH] Improve performance in RealmAssorter --- Rocket.Chat/Helpers/RealmAssorter.swift | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Rocket.Chat/Helpers/RealmAssorter.swift b/Rocket.Chat/Helpers/RealmAssorter.swift index 5059ca281c..4712aa15a1 100644 --- a/Rocket.Chat/Helpers/RealmAssorter.swift +++ b/Rocket.Chat/Helpers/RealmAssorter.swift @@ -21,7 +21,18 @@ class RealmAssorter { var objects: Results var section: ArraySection { - 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) } } @@ -62,7 +73,11 @@ class RealmAssorter { self.model?.invalidate() self.model = model.observe { _ in let oldValue = self.sections - let newValue = self.results.map { $0.section } + var newValue = [ArraySection]() + newValue.reserveCapacity(self.results.count) + for result in self.results { + newValue.append(result.section) + } let changes = StagedChangeset(source: oldValue, target: newValue)