Przeglądaj źródła

Changes for Concurrency

Marc G Fournier 3 lat temu
rodzic
commit
0ae2312c84

+ 6 - 4
FreeAPS/Sources/APS/APSManager.swift

@@ -671,9 +671,9 @@ final class BaseAPSManager: APSManager, Injectable {
             // Update the TDD value
             tdd(enacted_: enacted)
             // Update statistics. Only run if enabled in preferences
-            if settingsManager.settings.displayStatistics {
-                statistics()
-            }
+            // if settingsManager.settings.displayStatistics {
+            statistics()
+            // }
         }
     }
 
@@ -1336,7 +1336,9 @@ final class BaseAPSManager: APSManager, Injectable {
         nLS.end = loopStatRecord.end ?? Date()
         nLS.loopStatus = loopStatRecord.loopStatus
         nLS.duration = loopStatRecord.duration ?? 0.0
-        try? coredataContext.save()
+        coredataContext.perform {
+            try? self.coredataContext.save()
+        }
 
         print("Test time of LoopStats computation: \(-1 * LoopStatsStartedAt.timeIntervalSinceNow) s")
     }

+ 14 - 11
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -84,23 +84,26 @@ final class OpenAPS {
 
                     // MARK: Save to CoreData also. To do: Remove JSON saving
 
-                    self.coredataContext.performAndWait {
-                        var saveToTDD = TDD(context: self.coredataContext)
+                    var saveToTDD = TDD(context: self.coredataContext)
 
-                        if suggestion.tdd ?? 0 > 0 {
-                            // let saveToTDD = TDD(context: self.coredataContext)
-                            saveToTDD.timestamp = suggestion.timestamp ?? Date()
-                            saveToTDD.tdd = (suggestion.tdd ?? 0) as NSDecimalNumber?
+                    if suggestion.tdd ?? 0 > 0 {
+                        // let saveToTDD = TDD(context: self.coredataContext)
+                        saveToTDD.timestamp = suggestion.timestamp ?? Date()
+                        saveToTDD.tdd = (suggestion.tdd ?? 0) as NSDecimalNumber?
+                        self.coredataContext.perform {
                             try? self.coredataContext.save()
+                        }
 
-                            let saveToInsulin = InsulinDistribution(context: self.coredataContext)
-                            saveToInsulin.bolus = (suggestion.insulin?.bolus ?? 0) as NSDecimalNumber?
-                            saveToInsulin.scheduledBasal = (suggestion.insulin?.scheduled_basal ?? 0) as NSDecimalNumber?
-                            saveToInsulin.tempBasal = (suggestion.insulin?.temp_basal ?? 0) as NSDecimalNumber?
-                            saveToInsulin.date = Date()
+                        let saveToInsulin = InsulinDistribution(context: self.coredataContext)
+                        saveToInsulin.bolus = (suggestion.insulin?.bolus ?? 0) as NSDecimalNumber?
+                        saveToInsulin.scheduledBasal = (suggestion.insulin?.scheduled_basal ?? 0) as NSDecimalNumber?
+                        saveToInsulin.tempBasal = (suggestion.insulin?.temp_basal ?? 0) as NSDecimalNumber?
+                        saveToInsulin.date = Date()
+                        self.coredataContext.perform {
                             try? self.coredataContext.save()
                         }
                     }
+
                     promise(.success(suggestion))
                 } else {
                     promise(.success(nil))

+ 3 - 1
FreeAPS/Sources/APS/Storage/CarbsStorage.swift

@@ -50,7 +50,9 @@ final class BaseCarbsStorage: CarbsStorage, Injectable {
                 let carbDataForStats = Carbohydrates(context: coredataContext)
                 carbDataForStats.date = carbDate
                 carbDataForStats.carbs = cbs as NSDecimalNumber
-                try? coredataContext.save()
+                self.coredataContext.perform {
+                    try? self.coredataContext.save()
+                }
             }
 
             broadcaster.notify(CarbsObserver.self, on: processQueue) {

+ 3 - 1
FreeAPS/Sources/APS/Storage/GlucoseStorage.swift

@@ -72,7 +72,9 @@ final class BaseGlucoseStorage: GlucoseStorage, Injectable {
                     // let dataForStats = Readings(context: coredataContext)
                     dataForForStats.date = bgDate
                     dataForForStats.glucose = Int16(bg_)
-                    try? coredataContext.save()
+                    self.coredataContext.perform {
+                        try? self.coredataContext.save()
+                    }
                 }
                 // }
             }

+ 4 - 0
FreeAPS/Sources/Application/FreeAPSApp.swift

@@ -1,3 +1,4 @@
+import Firebase
 import SwiftUI
 import Swinject
 
@@ -43,6 +44,9 @@ import Swinject
     }
 
     init() {
+        // Use the Firebase library to configure APIs.
+        FirebaseApp.configure()
+
         debug(
             .default,
             "FreeAPS X Started: v\(Bundle.main.releaseVersionNumber ?? "")(\(Bundle.main.buildVersionNumber ?? "")) [buildDate: \(Bundle.main.buildDate)]"

+ 11 - 8
FreeAPS/Sources/Helpers/CoreDataStack.swift

@@ -19,14 +19,17 @@ class CoreDataStack {
 
     func saveContext() {
         let context = persistentContainer.viewContext
-        if context.hasChanges {
-            do {
-                try context.save()
-            } catch {
-                // Replace this implementation with code to handle the error appropriately.
-                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
-                let nserror = error as NSError
-                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
+
+        context.perform {
+            if context.hasChanges {
+                do {
+                    try context.save()
+                } catch {
+                    // Replace this implementation with code to handle the error appropriately.
+                    // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping    application, although it may be useful during development.
+                    let nserror = error as NSError
+                    fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
+                }
             }
         }
     }