From a296cb07500b1300385dae413149494eeb6d5905 Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Wed, 16 Mar 2022 13:10:34 +1100 Subject: Add refresh control + fix weird caching It might be my web server causing the weird caching with headers (it's just a json file on nginx). Changing the policy in the request seems to have fixed it though. Refresh control references: - https://stackoverflow.com/questions/24475792/how-to-use-pull-to-refresh-in-swift --- foray/Base.lproj/Main.storyboard | 3 +++ foray/ForayTableViewController.swift | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/foray/Base.lproj/Main.storyboard b/foray/Base.lproj/Main.storyboard index 17cfb05..b6b3f86 100644 --- a/foray/Base.lproj/Main.storyboard +++ b/foray/Base.lproj/Main.storyboard @@ -122,6 +122,9 @@ + + + diff --git a/foray/ForayTableViewController.swift b/foray/ForayTableViewController.swift index 67f0bc5..0643051 100644 --- a/foray/ForayTableViewController.swift +++ b/foray/ForayTableViewController.swift @@ -64,6 +64,20 @@ class ForayTableViewController: UITableViewController { alert.view.addSubview(loadingIndicator) present(alert, animated: true, completion: nil) + reloadApiData() + + dismiss(animated: false, completion: nil) + + // Not 100% sure what this does (the for: bit) + self.refreshControl?.addTarget(self, action: #selector(doRefresh), for: UIControl.Event.valueChanged) + } + + // Not sure why need @objc. Is it due to class private/public? + @objc func doRefresh(sender: AnyObject) { + reloadApiData() + } + + func reloadApiData() { loadApiData(onComplete: { (apiItems) in self.items = apiItems @@ -77,9 +91,8 @@ class ForayTableViewController: UITableViewController { self.sections.sort { (lhs, rhs) in lhs.year < rhs.year } self.tableView.reloadData() + self.refreshControl?.endRefreshing() }) - - dismiss(animated: false, completion: nil) } func loadApiData(onComplete: @escaping ([MyItem]) -> ()) { @@ -97,6 +110,7 @@ class ForayTableViewController: UITableViewController { var request = URLRequest(url: URL(string: "https://.../dummy.json")!) let authData = ("..:..").data(using: .utf8)!.base64EncodedString() request.addValue("Basic \(authData)", forHTTPHeaderField: "Authorization") + request.cachePolicy = .reloadRevalidatingCacheData // Needed otherwise default caching policy seems not to check properly URLSession.shared.dataTask(with: request, completionHandler: { data, response, error -> Void in print("finished getting data") -- cgit