Skip to content

Commit

Permalink
attempt 2 to fix crashes in StatisticsManager.calculateStatistics
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanDegraeve committed May 23, 2021
1 parent 77467f8 commit ad77092
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
13 changes: 4 additions & 9 deletions xdrip/Managers/Charts/GlucoseChartManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,6 @@ public final class GlucoseChartManager {
return
}

// intialize private managed object context
let managedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)

managedObjectContext.parent = self.coreDataManager.mainManagedObjectContext

// startDateToUse is either parameter value or (if nil), endDate minutes current chartwidth
let startDateToUse = startDate != nil ? startDate! : Date(timeInterval: -self.endDate.timeIntervalSince(self.startDate), since: endDate)

Expand Down Expand Up @@ -185,7 +180,7 @@ public final class GlucoseChartManager {
reUseExistingChartPointList = false

// use newGlucoseChartPointsToAppend and assign it to new list of chartpoints startDate to endDate
newGlucoseChartPointsToAppend = self.getGlucoseChartPoints(startDate: startDateToUse, endDate: endDate, bgReadingsAccessor: self.data().bgReadingsAccessor, on: managedObjectContext)
newGlucoseChartPointsToAppend = self.getGlucoseChartPoints(startDate: startDateToUse, endDate: endDate, bgReadingsAccessor: self.data().bgReadingsAccessor, on: self.coreDataManager.privateManagedObjectContext)

// lastChartPointEarlierThanEndDate is the last chartPoint int he array to append
self.lastChartPointEarlierThanEndDate = newGlucoseChartPointsToAppend.lastGlucoseChartPoint
Expand All @@ -206,7 +201,7 @@ public final class GlucoseChartManager {
} else {

// append glucseChartpoints with date > x.date up to endDate
newGlucoseChartPointsToAppend = self.getGlucoseChartPoints(startDate: lastGlucoseTimeStamp, endDate: endDate, bgReadingsAccessor: self.data().bgReadingsAccessor, on: managedObjectContext)
newGlucoseChartPointsToAppend = self.getGlucoseChartPoints(startDate: lastGlucoseTimeStamp, endDate: endDate, bgReadingsAccessor: self.data().bgReadingsAccessor, on: self.coreDataManager.privateManagedObjectContext)

// lastChartPointEarlierThanEndDate is the last chartPoint int he array to append
self.lastChartPointEarlierThanEndDate = newGlucoseChartPointsToAppend.lastGlucoseChartPoint
Expand All @@ -222,7 +217,7 @@ public final class GlucoseChartManager {

if let firstGlucoseChartPoint = self.glucoseChartPoints.firstGlucoseChartPoint, let firstGlucoseChartPointX = firstGlucoseChartPoint.x as? ChartAxisValueDate, startDateToUse < firstGlucoseChartPointX.date {

newGlucoseChartPointsToPrepend = self.getGlucoseChartPoints(startDate: startDateToUse, endDate: firstGlucoseChartPointX.date, bgReadingsAccessor: self.data().bgReadingsAccessor, on: managedObjectContext)
newGlucoseChartPointsToPrepend = self.getGlucoseChartPoints(startDate: startDateToUse, endDate: firstGlucoseChartPointX.date, bgReadingsAccessor: self.data().bgReadingsAccessor, on: self.coreDataManager.privateManagedObjectContext)

// maybe there's a higher value now for maximumValueInGlucoseChartPoints
self.maximumValueInGlucoseChartPointsInMgDl = self.getNewMaximumValueInGlucoseChartPoints(currentMaximumValueInGlucoseChartPoints: self.maximumValueInGlucoseChartPointsInMgDl, glucoseChartPoints: newGlucoseChartPointsToPrepend)
Expand All @@ -236,7 +231,7 @@ public final class GlucoseChartManager {
// this should be a case where there's no glucoseChartPoints stored yet, we just create a new array to append

// get glucosePoints from coredata
newGlucoseChartPointsToAppend = self.getGlucoseChartPoints(startDate: startDateToUse, endDate: endDate, bgReadingsAccessor: self.data().bgReadingsAccessor, on: managedObjectContext)
newGlucoseChartPointsToAppend = self.getGlucoseChartPoints(startDate: startDateToUse, endDate: endDate, bgReadingsAccessor: self.data().bgReadingsAccessor, on: self.coreDataManager.privateManagedObjectContext)

// lastChartPointEarlierThanEndDate is the last chartPoint int he array to append
self.lastChartPointEarlierThanEndDate = newGlucoseChartPointsToAppend.lastGlucoseChartPoint
Expand Down
2 changes: 1 addition & 1 deletion xdrip/Managers/CoreData/CoreDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class CoreDataManager {
return managedObjectContext
}()

private lazy var privateManagedObjectContext: NSManagedObjectContext = {
private(set) lazy var privateManagedObjectContext: NSManagedObjectContext = {
let managedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)

managedObjectContext.persistentStoreCoordinator = self.persistentStoreCoordinator
Expand Down
20 changes: 6 additions & 14 deletions xdrip/Managers/Statistics/StatisticsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public final class StatisticsManager {
// MARK: - private properties

/// BgReadingsAccessor instance
private var bgReadingsAccessor:BgReadingsAccessor?
private var bgReadingsAccessor:BgReadingsAccessor

/// used for calculating statistics on a background thread
private let operationQueue: OperationQueue
Expand All @@ -29,7 +29,7 @@ public final class StatisticsManager {
// set coreDataManager and bgReadingsAccessor
self.coreDataManager = coreDataManager
self.bgReadingsAccessor = BgReadingsAccessor(coreDataManager: coreDataManager)

// initialize operationQueue
operationQueue = OperationQueue()

Expand All @@ -53,11 +53,6 @@ public final class StatisticsManager {
return
}

// intialize private managed object context
let managedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)

managedObjectContext.parent = self.coreDataManager.mainManagedObjectContext

// declare variables/constants
let isMgDl: Bool = UserDefaults.standard.bloodGlucoseUnitIsMgDl
var glucoseValues: [Double] = []
Expand All @@ -73,13 +68,10 @@ public final class StatisticsManager {
var highLimitForTIR: Double = 0
var numberOfDaysUsed: Int = 0

// initialize bgReadingsAccessor
let bgReadingsAccessor = BgReadingsAccessor(coreDataManager: self.coreDataManager)

managedObjectContext.performAndWait {

self.coreDataManager.privateManagedObjectContext.performAndWait {

// lets get the readings from the bgReadingsAccessor
let readings = bgReadingsAccessor.getBgReadings(from: fromDate, to: toDate, on: managedObjectContext)
let readings = self.bgReadingsAccessor.getBgReadings(from: fromDate, to: toDate, on: self.coreDataManager.privateManagedObjectContext)

//if there are no available readings, return without doing anything
if readings.count == 0 {
Expand Down Expand Up @@ -216,6 +208,6 @@ public final class StatisticsManager {
var numberOfDaysUsed: Int

}

}

0 comments on commit ad77092

Please sign in to comment.