From 715a78e5eaebc4f27e80d5e07ccc2c2e253afe75 Mon Sep 17 00:00:00 2001 From: Matheus Cardoso Date: Thu, 6 Dec 2018 19:15:18 -0200 Subject: [PATCH 1/2] Map RealmAssorter sections in a background thread --- .../SubscriptionsViewModel.swift | 1 + Rocket.Chat/Helpers/RealmAssorter.swift | 41 +++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Rocket.Chat/Controllers/Subscriptions/SubscriptionsList/SubscriptionsViewModel.swift b/Rocket.Chat/Controllers/Subscriptions/SubscriptionsList/SubscriptionsViewModel.swift index f79048fc90..cb5d63bb48 100644 --- a/Rocket.Chat/Controllers/Subscriptions/SubscriptionsList/SubscriptionsViewModel.swift +++ b/Rocket.Chat/Controllers/Subscriptions/SubscriptionsList/SubscriptionsViewModel.swift @@ -72,6 +72,7 @@ class SubscriptionsViewModel { func buildSections() { if let realm = realm, assorter == nil { + // TODO: Make this realm instance in another thread assorter = RealmAssorter(realm: realm) assorter?.didUpdateIndexPaths = didUpdateIndexPaths } diff --git a/Rocket.Chat/Helpers/RealmAssorter.swift b/Rocket.Chat/Helpers/RealmAssorter.swift index 5059ca281c..6d3ce0516c 100644 --- a/Rocket.Chat/Helpers/RealmAssorter.swift +++ b/Rocket.Chat/Helpers/RealmAssorter.swift @@ -12,6 +12,7 @@ import DifferenceKit typealias IndexPathsChanges = (deletions: [IndexPath], insertions: [IndexPath], modifications: [IndexPath]) let subscriptionUpdatesHandlerQueue = DispatchQueue(label: "chat.rocket.subscription.updates.handler", qos: .background) +let subscriptionUpdatesHandlerQueue2 = DispatchQueue(label: "chat.rocket.subscription.updates.handler2", qos: .background) class RealmAssorter { typealias IndexPathsChangesEvent = (StagedChangeset<[ArraySection]>, (_ newData: [ArraySection]) -> Void) -> Void @@ -20,8 +21,30 @@ class RealmAssorter { let name: String var objects: Results - var section: ArraySection { - return ArraySection(model: name, elements: objects.compactMap { $0.unmanaged }) + func buildSection(completion: @escaping (ArraySection) -> Void) { + let objectIds: [String] = objects.compactMap { + return $0.value(forKeyPath: "identifier") as? String + } + + subscriptionUpdatesHandlerQueue2.async { + guard + let configuration = self.objects.realm?.configuration, + let realm = try? Realm(configuration: configuration) + else { + return + } + + let unmanagedObjects = objectIds.compactMap { + realm.object(ofType: Object.self, forPrimaryKey: $0)?.unmanaged + } + + completion( + ArraySection( + model: self.name, + elements: unmanagedObjects + ) + ) + } } } @@ -62,7 +85,19 @@ class RealmAssorter { self.model?.invalidate() self.model = model.observe { _ in let oldValue = self.sections - let newValue = self.results.map { $0.section } + var newValue: [ArraySection] = [] + let dispatchGroup = DispatchGroup() + + for result in self.results { + dispatchGroup.enter() + + result.buildSection(completion: { (section) in + newValue.append(section) + dispatchGroup.leave() + }) + } + + dispatchGroup.wait() let changes = StagedChangeset(source: oldValue, target: newValue) From 8374be41bd71f1dbc703d6dae84207b46b4c1eb2 Mon Sep 17 00:00:00 2001 From: Matheus Cardoso Date: Fri, 7 Dec 2018 01:09:11 -0200 Subject: [PATCH 2/2] Update dispatch queue name --- Rocket.Chat/Helpers/RealmAssorter.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Rocket.Chat/Helpers/RealmAssorter.swift b/Rocket.Chat/Helpers/RealmAssorter.swift index 6d3ce0516c..f9abd4c8ae 100644 --- a/Rocket.Chat/Helpers/RealmAssorter.swift +++ b/Rocket.Chat/Helpers/RealmAssorter.swift @@ -12,7 +12,7 @@ import DifferenceKit typealias IndexPathsChanges = (deletions: [IndexPath], insertions: [IndexPath], modifications: [IndexPath]) let subscriptionUpdatesHandlerQueue = DispatchQueue(label: "chat.rocket.subscription.updates.handler", qos: .background) -let subscriptionUpdatesHandlerQueue2 = DispatchQueue(label: "chat.rocket.subscription.updates.handler2", qos: .background) +let sectionBuildingQueue = DispatchQueue(label: "chat.rocket.section.building", qos: .background) class RealmAssorter { typealias IndexPathsChangesEvent = (StagedChangeset<[ArraySection]>, (_ newData: [ArraySection]) -> Void) -> Void @@ -26,7 +26,7 @@ class RealmAssorter { return $0.value(forKeyPath: "identifier") as? String } - subscriptionUpdatesHandlerQueue2.async { + sectionBuildingQueue.async { guard let configuration = self.objects.realm?.configuration, let realm = try? Realm(configuration: configuration)