Ensure all statuses are cached before returning

This commit is contained in:
Shadowfacts 2022-12-26 12:09:57 -05:00
parent 24b3fa1e3f
commit bb40894778
1 changed files with 10 additions and 2 deletions

View File

@ -353,16 +353,24 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
guard !unloaded.isEmpty else { guard !unloaded.isEmpty else {
return return
} }
await withTaskGroup(of: Void.self) { group in let statuses = await withTaskGroup(of: Status?.self) { group -> [Status] in
for id in unloaded { for id in unloaded {
group.addTask { @MainActor in group.addTask { @MainActor in
if let (status, _) = try? await self.mastodonController.run(Client.getStatus(id: id)) { if let (status, _) = try? await self.mastodonController.run(Client.getStatus(id: id)) {
self.mastodonController.persistentContainer.addOrUpdate(status: status) return status
} else {
return nil
} }
} }
} }
return await group.reduce(into: []) { partialResult, status in
if let status {
partialResult.append(status)
} }
} }
}
await mastodonController.persistentContainer.addAll(statuses: statuses)
}
private func applyItemsToRestore(position: TimelinePosition) { private func applyItemsToRestore(position: TimelinePosition) {
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>() var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()