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

Commit 715a78e

Browse files
committed
Map RealmAssorter sections in a background thread
1 parent 1192527 commit 715a78e

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

Rocket.Chat/Controllers/Subscriptions/SubscriptionsList/SubscriptionsViewModel.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class SubscriptionsViewModel {
7272

7373
func buildSections() {
7474
if let realm = realm, assorter == nil {
75+
// TODO: Make this realm instance in another thread
7576
assorter = RealmAssorter<Subscription>(realm: realm)
7677
assorter?.didUpdateIndexPaths = didUpdateIndexPaths
7778
}

Rocket.Chat/Helpers/RealmAssorter.swift

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import DifferenceKit
1212
typealias IndexPathsChanges = (deletions: [IndexPath], insertions: [IndexPath], modifications: [IndexPath])
1313

1414
let subscriptionUpdatesHandlerQueue = DispatchQueue(label: "chat.rocket.subscription.updates.handler", qos: .background)
15+
let subscriptionUpdatesHandlerQueue2 = DispatchQueue(label: "chat.rocket.subscription.updates.handler2", qos: .background)
1516

1617
class RealmAssorter<Object: RealmSwift.Object & UnmanagedConvertible> {
1718
typealias IndexPathsChangesEvent = (StagedChangeset<[ArraySection<String, Object.UnmanagedType>]>, (_ newData: [ArraySection<String, Object.UnmanagedType>]) -> Void) -> Void
@@ -20,8 +21,30 @@ class RealmAssorter<Object: RealmSwift.Object & UnmanagedConvertible> {
2021
let name: String
2122
var objects: Results<Object>
2223

23-
var section: ArraySection<String, Object.UnmanagedType> {
24-
return ArraySection(model: name, elements: objects.compactMap { $0.unmanaged })
24+
func buildSection(completion: @escaping (ArraySection<String, Object.UnmanagedType>) -> Void) {
25+
let objectIds: [String] = objects.compactMap {
26+
return $0.value(forKeyPath: "identifier") as? String
27+
}
28+
29+
subscriptionUpdatesHandlerQueue2.async {
30+
guard
31+
let configuration = self.objects.realm?.configuration,
32+
let realm = try? Realm(configuration: configuration)
33+
else {
34+
return
35+
}
36+
37+
let unmanagedObjects = objectIds.compactMap {
38+
realm.object(ofType: Object.self, forPrimaryKey: $0)?.unmanaged
39+
}
40+
41+
completion(
42+
ArraySection(
43+
model: self.name,
44+
elements: unmanagedObjects
45+
)
46+
)
47+
}
2548
}
2649
}
2750

@@ -62,7 +85,19 @@ class RealmAssorter<Object: RealmSwift.Object & UnmanagedConvertible> {
6285
self.model?.invalidate()
6386
self.model = model.observe { _ in
6487
let oldValue = self.sections
65-
let newValue = self.results.map { $0.section }
88+
var newValue: [ArraySection<String, Object.UnmanagedType>] = []
89+
let dispatchGroup = DispatchGroup()
90+
91+
for result in self.results {
92+
dispatchGroup.enter()
93+
94+
result.buildSection(completion: { (section) in
95+
newValue.append(section)
96+
dispatchGroup.leave()
97+
})
98+
}
99+
100+
dispatchGroup.wait()
66101

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

0 commit comments

Comments
 (0)