aboutsummaryrefslogtreecommitdiff
path: root/foray/Presenters
diff options
context:
space:
mode:
authorNicholas Tay <nick@windblume.net>2022-07-17 02:13:04 +1000
committerNicholas Tay <nick@windblume.net>2022-07-17 02:13:16 +1000
commit1adbed9f8b94521befd237c14d36325a55037a41 (patch)
tree91d81b4740514083010c72f5f0559283ccd849fa /foray/Presenters
parentdaee1f1b5c739f42ba54a1ebbb9655f5034e315f (diff)
downloadforayios-1adbed9f8b94521befd237c14d36325a55037a41.tar.gz
forayios-1adbed9f8b94521befd237c14d36325a55037a41.tar.bz2
forayios-1adbed9f8b94521befd237c14d36325a55037a41.zip
Experiment with async/await
Note that URLSession async only works >=iOS 15. I changed the target for now, but may mess with continuations.
Diffstat (limited to 'foray/Presenters')
-rw-r--r--foray/Presenters/PenguinItemPresenter.swift19
1 files changed, 10 insertions, 9 deletions
diff --git a/foray/Presenters/PenguinItemPresenter.swift b/foray/Presenters/PenguinItemPresenter.swift
index c4553ae..1d617bf 100644
--- a/foray/Presenters/PenguinItemPresenter.swift
+++ b/foray/Presenters/PenguinItemPresenter.swift
@@ -8,18 +8,19 @@
import Foundation
class PenguinItemPresenter {
+
+ private struct Constants {
+ static let apiEndpoint = "https://users.windblume.net/~nick/upload/dummy.json"
+ }
let fetcher = ForayFetcher()
- func fetch(receiver: @escaping ([PenguinItemViewModel]) -> ()) {
- 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))
- }
+ func fetch() async -> [PenguinItemViewModel] {
+ do {
+ let apiItems: [PenguinItemModel] = try await fetcher.fetch(url: Constants.apiEndpoint)
+ return transform(models: apiItems)
+ } catch {
+ return []
}
}