From b8e136005a3632fd4825e065ae7eb3e6742a6b90 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Fri, 9 Sep 2022 17:47:49 -0500 Subject: [PATCH] Automatically purges LoggedErrors older than one month Occurs whenever app enters background. --- AltStore/AppDelegate.swift | 13 +++++++++++++ AltStore/SceneDelegate.swift | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/AltStore/AppDelegate.swift b/AltStore/AppDelegate.swift index dbbf160f1..951657517 100644 --- a/AltStore/AppDelegate.swift +++ b/AltStore/AppDelegate.swift @@ -98,7 +98,20 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func applicationDidEnterBackground(_ application: UIApplication) { + // Make sure to update SceneDelegate.sceneDidEnterBackground() as well. + ServerManager.shared.stopDiscovering() + + guard let oneMonthAgo = Calendar.current.date(byAdding: .month, value: -1, to: Date()) else { return } + + let midnightOneMonthAgo = Calendar.current.startOfDay(for: oneMonthAgo) + DatabaseManager.shared.purgeLoggedErrors(before: midnightOneMonthAgo) { result in + switch result + { + case .success: break + case .failure(let error): print("[ALTLog] Failed to purge logged errors before \(midnightOneMonthAgo).", error) + } + } } func applicationWillEnterForeground(_ application: UIApplication) diff --git a/AltStore/SceneDelegate.swift b/AltStore/SceneDelegate.swift index 2249c1c5f..812564695 100644 --- a/AltStore/SceneDelegate.swift +++ b/AltStore/SceneDelegate.swift @@ -52,7 +52,20 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate guard UIApplication.shared.applicationState == .background else { return } + // Make sure to update AppDelegate.applicationDidEnterBackground() as well. + ServerManager.shared.stopDiscovering() + + guard let oneMonthAgo = Calendar.current.date(byAdding: .month, value: -1, to: Date()) else { return } + + let midnightOneMonthAgo = Calendar.current.startOfDay(for: oneMonthAgo) + DatabaseManager.shared.purgeLoggedErrors(before: midnightOneMonthAgo) { result in + switch result + { + case .success: break + case .failure(let error): print("[ALTLog] Failed to purge logged errors before \(midnightOneMonthAgo).", error) + } + } } func scene(_ scene: UIScene, openURLContexts URLContexts: Set)