From b85e69959b890844a9f855e3e4130f5a2275e891 Mon Sep 17 00:00:00 2001 From: Dominik Kapusta Date: Fri, 29 Nov 2024 14:14:30 +0100 Subject: [PATCH] Reset context after executing NSBatchDeleteRequest --- Sources/PrivacyStats/PrivacyStats.swift | 12 +++++------- .../PrivacyStats/internal/PrivacyStatsUtils.swift | 2 ++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Sources/PrivacyStats/PrivacyStats.swift b/Sources/PrivacyStats/PrivacyStats.swift index 325f77cb2..9f9944398 100644 --- a/Sources/PrivacyStats/PrivacyStats.swift +++ b/Sources/PrivacyStats/PrivacyStats.swift @@ -201,11 +201,6 @@ public final class PrivacyStats: PrivacyStatsCollecting { } } - // When storing a pack from a previous day, we may have outdated packs, so delete them as needed. - if !isCurrentDayPack { - try PrivacyStatsUtils.deleteOutdatedPacks(in: context) - } - guard context.hasChanges else { continuation.resume() return @@ -214,10 +209,13 @@ public final class PrivacyStats: PrivacyStatsCollecting { try context.save() Logger.privacyStats.debug("Saved stats \(pack.timestamp) \(pack.trackers)") - // Only emit update event when saving current-day pack. For previous-day pack, - // a follow-up commit event will come and we'll emit the update then. if isCurrentDayPack { + // Only emit update event when saving current-day pack. For previous-day pack, + // a follow-up commit event will come and we'll emit the update then. statsUpdateSubject.send() + } else { + // When storing a pack from a previous day, we may have outdated packs, so delete them as needed. + try PrivacyStatsUtils.deleteOutdatedPacks(in: context) } } catch { Logger.privacyStats.error("Save error: \(error)") diff --git a/Sources/PrivacyStats/internal/PrivacyStatsUtils.swift b/Sources/PrivacyStats/internal/PrivacyStatsUtils.swift index d6ceb3032..33b93d869 100644 --- a/Sources/PrivacyStats/internal/PrivacyStatsUtils.swift +++ b/Sources/PrivacyStats/internal/PrivacyStatsUtils.swift @@ -109,6 +109,7 @@ final class PrivacyStatsUtils { let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) try context.execute(deleteRequest) + context.reset() } /** @@ -117,5 +118,6 @@ final class PrivacyStatsUtils { static func deleteAllStats(in context: NSManagedObjectContext) throws { let deleteRequest = NSBatchDeleteRequest(fetchRequest: DailyBlockedTrackersEntity.fetchRequest()) try context.execute(deleteRequest) + context.reset() } }