Automatically purges LoggedErrors older than one month

Occurs whenever app enters background.
This commit is contained in:
Riley Testut
2022-09-09 17:47:49 -05:00
committed by Joseph Mattello
parent d2b419c42e
commit f472b227bb
2 changed files with 93 additions and 69 deletions

View File

@@ -96,9 +96,21 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
} }
func applicationDidEnterBackground(_ application: UIApplication) func applicationDidEnterBackground(_ application: UIApplication)
{ {
// Make sure to update SceneDelegate.sceneDidEnterBackground() as well.
} 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) func applicationWillEnterForeground(_ application: UIApplication)
{ {

View File

@@ -53,6 +53,18 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate
guard UIApplication.shared.applicationState == .background else { return } guard UIApplication.shared.applicationState == .background else { return }
// Make sure to update AppDelegate.applicationDidEnterBackground() as well.
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)
}
}
} }