diff options
-rw-r--r-- | LEARNING.md | 2 | ||||
-rw-r--r-- | foray/Presenters/PenguinItemPresenter.swift | 3 | ||||
-rw-r--r-- | foray/Scenes/ForayTableViewController.swift | 6 |
3 files changed, 7 insertions, 4 deletions
diff --git a/LEARNING.md b/LEARNING.md index 2c04f65..9259fea 100644 --- a/LEARNING.md +++ b/LEARNING.md @@ -26,6 +26,6 @@ - [x] Presenter - VC shouldn't see PenguinItem, only the one for rendering, e.g. 'PenguinItemModel' - ViewModel struct can be together with Presenter file if simple -- [ ] Background thread +- [x] Background thread - DispatchQueue backgroundThread - Just one place on the presenter to fire back on Main diff --git a/foray/Presenters/PenguinItemPresenter.swift b/foray/Presenters/PenguinItemPresenter.swift index 698b56e..c4553ae 100644 --- a/foray/Presenters/PenguinItemPresenter.swift +++ b/foray/Presenters/PenguinItemPresenter.swift @@ -12,11 +12,12 @@ class PenguinItemPresenter { let fetcher = ForayFetcher() func fetch(receiver: @escaping ([PenguinItemViewModel]) -> ()) { - fetcher.fetch(url: "https://users.windblume.net/~nick/upload/dummy.json") { (apiItems: [PenguinItemModel]) in + fetcher.fetch(url: "https://users.windblume.net/~nick/upload/dummy.json") { [weak self] (apiItems: [PenguinItemModel]) in // Callback to main thread here // There probably is a nicer way to do it, but we will DispatchQueue it back // from the Presenter-level for now (main thread from VC onwards) DispatchQueue.main.async { + guard let self = self else { return } receiver(self.transform(models: apiItems)) } } diff --git a/foray/Scenes/ForayTableViewController.swift b/foray/Scenes/ForayTableViewController.swift index 13b9dea..a29088c 100644 --- a/foray/Scenes/ForayTableViewController.swift +++ b/foray/Scenes/ForayTableViewController.swift @@ -47,7 +47,9 @@ class ForayTableViewController: UITableViewController, Coordinated { } func reloadApiData() { - presenter.fetch(receiver: { (data: [PenguinItemViewModel]) in + presenter.fetch { [weak self] (data: [PenguinItemViewModel]) in + guard let self = self else { return } + let groups = Dictionary(grouping: data) { $0.year } self.sections = groups.map { (key, values) in return YearSection(year: key, items: values) @@ -58,7 +60,7 @@ class ForayTableViewController: UITableViewController, Coordinated { self.tableView.reloadData() self.refreshControl?.endRefreshing() self.coordinator?.hideLoading() - }) + } } // MARK: - Table view data source |