From 7c5f20af5d5687b81a552ef521f11d322b31b415 Mon Sep 17 00:00:00 2001 From: Johan Degraeve Date: Sat, 30 Apr 2022 11:36:33 +0200 Subject: [PATCH] treatments : delete treatments older than 90 days deletion is done at app start up. If user would go to treatments screen very fastly, after starting the app, then it may crash (only once). But that's probably not going to happen frequently --- xdrip/Constants/ConstantsHousekeeping.swift | 4 +- .../Utilities/HouseKeeping/HouseKeeper.swift | 45 +++++++++++++++++-- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/xdrip/Constants/ConstantsHousekeeping.swift b/xdrip/Constants/ConstantsHousekeeping.swift index 73d376597..354726a42 100644 --- a/xdrip/Constants/ConstantsHousekeeping.swift +++ b/xdrip/Constants/ConstantsHousekeeping.swift @@ -2,7 +2,7 @@ import Foundation enum ConstantsHousekeeping { - /// how long to keep bgReadings can calibrations - static let retentionPeriodBgReadingsAndCalibrationsInDays = 90.0 + /// how long to keep bgReadings can calibrations and treatments + static let retentionPeriodBgReadingsAndCalibrationsAndTreatmentsInDays = 90.0 } diff --git a/xdrip/Utilities/HouseKeeping/HouseKeeper.swift b/xdrip/Utilities/HouseKeeping/HouseKeeper.swift index bd932d310..b9f8a58d2 100644 --- a/xdrip/Utilities/HouseKeeping/HouseKeeper.swift +++ b/xdrip/Utilities/HouseKeeping/HouseKeeper.swift @@ -16,6 +16,9 @@ class HouseKeeper { /// CalibrationsAccessor instance private var calibrationsAccessor:CalibrationsAccessor + /// TreatmentEntryAccessor instance + private var treatmentsEntryAccessor: TreatmentEntryAccessor + /// CoreDataManager instance private var coreDataManager: CoreDataManager @@ -30,9 +33,11 @@ class HouseKeeper { self.calibrationsAccessor = CalibrationsAccessor(coreDataManager: coreDataManager) + self.treatmentsEntryAccessor = TreatmentEntryAccessor(coreDataManager: coreDataManager) + self.coreDataManager = coreDataManager - self.toDate = Date(timeIntervalSinceNow: -ConstantsHousekeeping.retentionPeriodBgReadingsAndCalibrationsInDays*24*3600) + self.toDate = Date(timeIntervalSinceNow: -ConstantsHousekeeping.retentionPeriodBgReadingsAndCalibrationsAndTreatmentsInDays*24*3600) } @@ -62,11 +67,19 @@ class HouseKeeper { } + // delete delete OldTreatments on the private managedObjectContext, asynchronously + managedObjectContext.perform { + + // delete delete OldTreatments + self.deleteOldTreatments(on: managedObjectContext) + + } + } // MARK: - private functions - /// deletes old readings. Readings older than ConstantsHousekeeping.retentionPeriodBgReadingsInDays will be deleted + /// deletes old readings. Readings older than ConstantsHousekeeping.retentionPeriodBgReadingsAndCalibrationsAndTreatmentsInDays will be deleted /// - managedObjectContext : the ManagedObjectContext to use private func deleteOldReadings(on managedObjectContext: NSManagedObjectContext) { @@ -90,7 +103,7 @@ class HouseKeeper { } - /// deletes old calibrations. Readings older than ConstantsHousekeeping.retentionPeriodBgReadingsInDays will be deleted + /// deletes old calibrations. Readings older than ConstantsHousekeeping.retentionPeriodBgReadingsAndCalibrationsAndTreatmentsInDays will be deleted private func deleteOldCalibrations(on managedObjectContext: NSManagedObjectContext) { // get old calibrations to delete @@ -120,5 +133,31 @@ class HouseKeeper { } } + + /// deletes old treatments. Treatments older than ConstantsHousekeeping.retentionPeriodBgReadingsAndCalibrationsAndTreatmentsInDays will be deleted + /// - managedObjectContext : the ManagedObjectContext to use + private func deleteOldTreatments(on managedObjectContext: NSManagedObjectContext) { + + // get old treatments to delete + let oldTreatments = self.treatmentsEntryAccessor.getTreatments(fromDate: nil, toDate: Date(timeIntervalSinceNow: -ConstantsHousekeeping.retentionPeriodBgReadingsAndCalibrationsAndTreatmentsInDays*24*3600), on: managedObjectContext) + + if oldTreatments.count > 0 { + + trace("in deleteOldTreatments, number of treatments to delete : %{public}@, to date = %{public}@", log: self.log, category: ConstantsLog.categoryHouseKeeper, type: .info, oldTreatments.count.description, self.toDate.description(with: .current)) + + } + + // delete them + for oldTreatment in oldTreatments { + + treatmentsEntryAccessor.delete(treatmentEntry: oldTreatment, on: managedObjectContext) + + coreDataManager.saveChanges() + + } + + } + + }