Skip to content

Commit

Permalink
treatments : delete treatments older than 90 days
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JohanDegraeve committed Apr 30, 2022
1 parent 8069c00 commit 7c5f20a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
4 changes: 2 additions & 2 deletions xdrip/Constants/ConstantsHousekeeping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
45 changes: 42 additions & 3 deletions xdrip/Utilities/HouseKeeping/HouseKeeper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class HouseKeeper {
/// CalibrationsAccessor instance
private var calibrationsAccessor:CalibrationsAccessor

/// TreatmentEntryAccessor instance
private var treatmentsEntryAccessor: TreatmentEntryAccessor

/// CoreDataManager instance
private var coreDataManager: CoreDataManager

Expand All @@ -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)

}

Expand Down Expand Up @@ -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) {

Expand All @@ -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
Expand Down Expand Up @@ -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()

}

}



}

0 comments on commit 7c5f20a

Please sign in to comment.