From 1adbed9f8b94521befd237c14d36325a55037a41 Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Sun, 17 Jul 2022 02:13:04 +1000 Subject: Experiment with async/await Note that URLSession async only works >=iOS 15. I changed the target for now, but may mess with continuations. --- foray/Scenes/ForayTableViewController.swift | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'foray/Scenes') diff --git a/foray/Scenes/ForayTableViewController.swift b/foray/Scenes/ForayTableViewController.swift index a29088c..849553c 100644 --- a/foray/Scenes/ForayTableViewController.swift +++ b/foray/Scenes/ForayTableViewController.swift @@ -42,21 +42,25 @@ class ForayTableViewController: UITableViewController, Coordinated { self.refreshControl?.addTarget(self, action: #selector(doRefresh), for: UIControl.Event.valueChanged) } - @objc func doRefresh(sender: AnyObject) { + @objc + func doRefresh(sender: AnyObject) { reloadApiData() } func reloadApiData() { - presenter.fetch { [weak self] (data: [PenguinItemViewModel]) in - guard let self = self else { return } - - let groups = Dictionary(grouping: data) { $0.year } + Task(priority: .medium) { [weak self] in + let models = await self?.presenter.fetch() + + guard let self = self, + let models = models else { return } + + let groups = Dictionary(grouping: models) { $0.year } self.sections = groups.map { (key, values) in return YearSection(year: key, items: values) } // Sort the sections from oldest year to newest self.sections.sort { (lhs, rhs) in lhs.year < rhs.year } - + self.tableView.reloadData() self.refreshControl?.endRefreshing() self.coordinator?.hideLoading() -- cgit