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,15 +353,23 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
guard !unloaded.isEmpty else {
return
}
await withTaskGroup(of: Void.self) { group in
let statuses = await withTaskGroup(of: Status?.self) { group -> [Status] in
for id in unloaded {
group.addTask { @MainActor in
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) {