Skip to content

Commit 6e30a5f

Browse files
committed
Add cleanup for cellular data usage records
Removes records older than 60 days during sync to prevent unbounded database growth.
1 parent 5987c87 commit 6e30a5f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

podcasts/Constants.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ struct Constants {
186186
static let firstTimePlaylistCreated = "FirstTimePlaylistCreated"
187187
static let saveCurrentUpNextQueueIntoPlaylist = "SaveCurrentUpNextQueueIntoPlaylist"
188188
static let shouldResultEndOfYearSyncStatus = "ShouldResultEndOfYearSyncStatus"
189+
static let lastCellularDataUsageCleanupDate = "lastCellularDataUsageCleanupDate"
189190

190191
enum headphones {
191192
static let previousAction = SettingValue("headphones.previousAction",

podcasts/ServerSyncManager.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import PocketCastsUtils
55

66
class ServerSyncManager: ServerSyncDelegate {
77
static let shared = ServerSyncManager()
8+
private static let cellularDataUsageRetentionPeriod: TimeInterval = 60.days
9+
private static let cellularDataUsageCleanupInterval: TimeInterval = 24.hours
810

911
// MARK: - Podcast functions
1012

@@ -73,6 +75,7 @@ class ServerSyncManager: ServerSyncDelegate {
7375
}
7476

7577
func performActionsAfterSync() {
78+
cleanupCellularDataUsageIfNeeded()
7679
PodcastManager.shared.checkForExpiredPodcastsAndCleanup()
7780
PodcastManager.shared.checkForPendingAndAutoDownloads()
7881
#if !APPCLIP
@@ -90,6 +93,28 @@ class ServerSyncManager: ServerSyncDelegate {
9093
}
9194
}
9295

96+
private func cleanupCellularDataUsageIfNeeded() {
97+
let defaults = UserDefaults.standard
98+
let lastCleanupDate = defaults.object(forKey: Constants.UserDefaults.lastCellularDataUsageCleanupDate) as? Date
99+
100+
guard DateUtil.hasEnoughTimePassed(since: lastCleanupDate, time: Self.cellularDataUsageCleanupInterval) else {
101+
return
102+
}
103+
104+
let cleanupDate = Date()
105+
defaults.set(cleanupDate, forKey: Constants.UserDefaults.lastCellularDataUsageCleanupDate)
106+
107+
Task {
108+
let didCleanup = await DataManager.sharedManager.cellularDataUsageManager.deleteRecords(
109+
olderThan: Date(timeIntervalSinceNow: -Self.cellularDataUsageRetentionPeriod)
110+
)
111+
112+
if !didCleanup {
113+
defaults.set(lastCleanupDate, forKey: Constants.UserDefaults.lastCellularDataUsageCleanupDate)
114+
}
115+
}
116+
}
117+
93118
func cleanupCloudOnlyFiles() {
94119
#if !APPCLIP
95120
UserEpisodeManager.cleanupCloudOnlyFiles()

0 commit comments

Comments
 (0)