Skip to content

Commit

Permalink
Reset context after executing NSBatchDeleteRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoy committed Nov 29, 2024
1 parent f291fab commit b85e699
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 5 additions & 7 deletions Sources/PrivacyStats/PrivacyStats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)")
Expand Down
2 changes: 2 additions & 0 deletions Sources/PrivacyStats/internal/PrivacyStatsUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ final class PrivacyStatsUtils {
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)

try context.execute(deleteRequest)
context.reset()
}

/**
Expand All @@ -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()
}
}

0 comments on commit b85e699

Please sign in to comment.