Add function to fetch stats. Add function to clean up iCloud storage if needed (runs once a day at most).

This commit is contained in:
Brent Simmons committed 2026-03-21 20:51:46 -07:00
1 parent 1766a4b819
commit cb70e717ab
1 file changed
+40 -2
@@ -116,7 +116,7 @@ enum CloudKitAccountDelegateError: LocalizedError, Sendable {
await withCheckedContinuation { continuation in
let op = CloudKitRemoteNotificationOperation(accountZone: accountZone, articlesZone: articlesZone, userInfo: userInfo)
op.completionBlock = { _ in
Self.logger.debug("CloutKitAccountDelegate: \(#function, privacy: .public) did complete")
Self.logger.debug("CloudKitAccountDelegate: \(#function, privacy: .public) did complete")
continuation.resume()
}
mainThreadOperationQueue.add(op)
@@ -136,13 +136,14 @@ enum CloudKitAccountDelegateError: LocalizedError, Sendable {
Self.logger.debug("CloudKitAccountDelegate: \(#function, privacy: .public)")
try await standardRefreshAll(for: account)
Self.logger.debug("CloutKitAccountDelegate: \(#function, privacy: .public) did complete")
Self.logger.debug("CloudKitAccountDelegate: \(#function, privacy: .public) did complete")
}
func syncArticleStatus(for account: Account) async throws {
Self.logger.debug("CloudKitAccountDelegate: \(#function, privacy: .public)")
try await sendArticleStatus(for: account)
try await refreshArticleStatus(for: account)
await cleanUpContentRecordsIfNeeded()
Self.logger.debug("CloudKitAccountDelegate: \(#function, privacy: .public) did complete")
}
@@ -497,6 +498,13 @@ enum CloudKitAccountDelegateError: LocalizedError, Sendable {
}
}
func fetchCloudKitStats(progress: @escaping CloudKitStatsProgressHandler) async throws -> CloudKitStats {
guard let account else {
throw CloudKitAccountDelegateError.unknown
}
return try await articlesZone.fetchZoneStats(account: account, progress: progress)
}
// MARK: - Suspend and Resume (for iOS)
func suspendNetwork() {
@@ -832,6 +840,36 @@ private extension CloudKitAccountDelegate {
throw error
}
}
// MARK: - Record Cleanup
static let lastCleanUpKey = "cloudkit.lastCleanUpDate"
func cleanUpContentRecordsIfNeeded() async {
let lastCleanUp = UserDefaults.standard.object(forKey: Self.lastCleanUpKey) as? Date ?? .distantPast
let oneDayAgo = Date(timeIntervalSinceNow: -24 * 60 * 60)
guard lastCleanUp < oneDayAgo else {
return
}
guard let account else {
return
}
// Set this unconditionally. If it fails, we don’t want to keep trying, possibly
// doing a bunch of extra work that will fail. Let it rest until the next go.
UserDefaults.standard.set(Date(), forKey: Self.lastCleanUpKey)
Self.logger.info("CloudKitAccountDelegate: running daily record cleanup")
do {
let syncUnreadContent = Self.syncArticleContentForUnreadArticles
let deleted = try await articlesZone.cleanUpRecords(account: account, syncUnreadContent: syncUnreadContent)
Self.logger.info("CloudKitAccountDelegate: daily cleanup deleted \(deleted, privacy: .public) records")
} catch {
Self.logger.error("CloudKitAccountDelegate: daily cleanup error: \(error.localizedDescription, privacy: .public)")
postSyncError(error, account: account, operation: "Daily record cleanup")
}
}
}
extension CloudKitAccountDelegate: LocalAccountRefresherDelegate {