From a824107ef6df7ca4fbecf4338c8e56066054a221 Mon Sep 17 00:00:00 2001 From: Jeff Hokit Date: Thu, 6 Jul 2023 13:02:14 -0700 Subject: [PATCH 1/2] always load cache at startup --- Shared/Data/NetworkManager.swift | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/Shared/Data/NetworkManager.swift b/Shared/Data/NetworkManager.swift index d5f74c2..3454214 100644 --- a/Shared/Data/NetworkManager.swift +++ b/Shared/Data/NetworkManager.swift @@ -106,17 +106,32 @@ class NetworkManager:ObservableObject // Convert into an array of Launch objects private func loadLaunches(_ timePeriod:TimePeriod) async { - // if the cached data is usable process that data, otherwise download new data - if cacheIsUsable(timePeriod){ - if launchesEmpty(timePeriod) // no need to reload existing cached data + // if no data is available, probably startup or first run + if launchesEmpty(timePeriod) + { + // if ANY cache is available, even if old + if cacheIsAvailable(timePeriod) { + // load the cache, even if old loadFromCache(timePeriod) } } - else { + + // check if cache is old, begin to load from network + if !cacheIsUsable(timePeriod) + { await loadFromNetwork(timePeriod) } } + + // return true if there is some data in the cache + func cacheIsAvailable(_ timePeriod:TimePeriod)->Bool { + + let cacheSize = timePeriod == .upcoming ? upcomingDataCache.count : recentDataCache.count + + return cacheSize > 1 + } + // return true if the cache for timePeriod is less than refreshInterval minutes old, and there is some data in the cache func cacheIsUsable(_ timePeriod:TimePeriod)->Bool { @@ -129,7 +144,6 @@ class NetworkManager:ObservableObject // load from cached data for timePeriod func loadFromCache(_ timePeriod:TimePeriod) { - print("good cache found") let data = timePeriod == .upcoming ? upcomingDataCache : recentDataCache let myLaunches = try! JSONDecoder().decode(LaunchReplies.self, from: data) From 26320e305a87ef56889d74493a2fd528ba203246 Mon Sep 17 00:00:00 2001 From: Jeff Hokit Date: Fri, 7 Jul 2023 09:06:39 -0700 Subject: [PATCH 2/2] fix: suppress API limit alert --- Shared/Data/NetworkManager.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Shared/Data/NetworkManager.swift b/Shared/Data/NetworkManager.swift index 3454214..15b9371 100644 --- a/Shared/Data/NetworkManager.swift +++ b/Shared/Data/NetworkManager.swift @@ -178,7 +178,13 @@ class NetworkManager:ObservableObject guard (200...299).contains(status) else { if (status == 429) { // handle "Too Many Requests" error - self.prepareAlert(title: "Too Many Requests", message: HTTPURLResponse.localizedString(forStatusCode: status)) + // Do not show an alert for this error. Because launches uses the free version + // of the API, this error can rise often with multiple users on the same IP address. + // Instead, just fail silently, hopefully displaying some cached data. + // A better solution would be to show a small note somewhere in the display + // indicating that the data is stale. + // Even better, paying for API access. + //self.prepareAlert(title: "Too Many Requests", message: HTTPURLResponse.localizedString(forStatusCode: status)) } else { // handle generic error