polscm32 2 anni fa
parent
commit
5a28fc077a

+ 1 - 1
FreeAPS.xcodeproj/xcshareddata/xcschemes/FreeAPS X.xcscheme

@@ -63,7 +63,7 @@
       <CommandLineArguments>
          <CommandLineArgument
             argument = "-com.apple.CoreData.ConcurrencyDebug 4"
-            isEnabled = "YES">
+            isEnabled = "NO">
          </CommandLineArgument>
       </CommandLineArguments>
       <EnvironmentVariables>

File diff suppressed because it is too large
+ 30 - 43
FreeAPS/Sources/APS/APSManager.swift


+ 5 - 11
FreeAPS/Sources/APS/DeviceDataManager.swift

@@ -355,17 +355,11 @@ extension BaseDeviceDataManager: PumpManagerDelegate {
             batteryToStore.voltage = nil
             batteryToStore.status = batteryPercent > 10 ? "normal" : "low"
             batteryToStore.display = status.pumpBatteryChargeRemaining != nil
-            if self.privateContext.hasChanges {
-                do {
-                    try self.privateContext.save()
-                    debugPrint(
-                        "Device Data manager: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved battery infos to core data"
-                    )
-                } catch {
-                    debugPrint(
-                        "Device Data manager: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save battery infos to core data"
-                    )
-                }
+
+            do {
+                try CoreDataStack.shared.backgroundContext.saveContext()
+            } catch {
+                print(error.localizedDescription)
             }
         }
         broadcaster.notify(PumpTimeZoneObserver.self, on: processQueue) {

+ 20 - 21
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -82,15 +82,10 @@ final class OpenAPS {
     }
 
     func attemptToSaveContext() {
-        if context.hasChanges {
-            do {
-                try context.save()
-                debugPrint("OpenAPS: \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved determination")
-            } catch {
-                debugPrint(
-                    "OpenAPS: \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) error while saving determination: \(error.localizedDescription)"
-                )
-            }
+        do {
+            try CoreDataStack.shared.backgroundContext.saveContext()
+        } catch {
+            print(error.localizedDescription)
         }
     }
 
@@ -238,14 +233,18 @@ final class OpenAPS {
 
                             saveToTDD.timestamp = determination.timestamp ?? Date()
                             saveToTDD.tdd = (determination.tdd ?? 0) as NSDecimalNumber?
-                            if self.context.hasChanges {
-                                try? self.context.save()
+                            do {
+                                try CoreDataStack.shared.backgroundContext.saveContext()
+                            } catch {
+                                print(error.localizedDescription)
                             }
 
                             let saveTarget = Target(context: self.context)
                             saveTarget.current = (determination.current_target ?? 100) as NSDecimalNumber?
-                            if self.context.hasChanges {
-                                try? self.context.save()
+                            do {
+                                try CoreDataStack.shared.backgroundContext.saveContext()
+                            } catch {
+                                print(error.localizedDescription)
                             }
                         }
                     }
@@ -347,8 +346,10 @@ final class OpenAPS {
                     saveToCoreData.duration = 0
                     saveToCoreData.indefinite = false
                     saveToCoreData.percentage = 100
-                    if self.context.hasChanges {
-                        try? self.context.save()
+                    do {
+                        try CoreDataStack.shared.backgroundContext.saveContext()
+                    } catch {
+                        print(error.localizedDescription)
                     }
                 }
             }
@@ -823,12 +824,10 @@ final class OpenAPS {
                 self.createForecast(type: type, values: values, date: currentDate, context: self.context)
             }
 
-            if self.context.hasChanges {
-                do {
-                    try self.context.save()
-                } catch {
-                    print("Failed to save forecast: \(error)")
-                }
+            do {
+                try CoreDataStack.shared.backgroundContext.saveContext()
+            } catch {
+                print(error.localizedDescription)
             }
         }
     }

+ 4 - 11
FreeAPS/Sources/APS/Storage/CarbsStorage.swift

@@ -152,17 +152,10 @@ final class BaseCarbsStorage: CarbsStorage, Injectable {
             newItem.carbs = Double(truncating: NSDecimalNumber(decimal: entry.carbs))
             newItem.id = UUID()
             newItem.isFPU = false
-            if self.coredataContext.hasChanges {
-                do {
-                    try self.coredataContext.save()
-                    debugPrint(
-                        "Carbs Storage: \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved carbs to core data"
-                    )
-                } catch {
-                    debugPrint(
-                        "Carbs Storage: \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) error while saving carbs to core data"
-                    )
-                }
+            do {
+                try CoreDataStack.shared.backgroundContext.saveContext()
+            } catch {
+                print(error.localizedDescription)
             }
         }
     }

+ 16 - 44
FreeAPS/Sources/APS/Storage/PumpHistoryStorage.swift

@@ -56,17 +56,10 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
                         newBolusEntry.isSMB = dose.automatic ?? true
                         // TODO: - do we need duration here?
 
-                        if self.context.hasChanges {
-                            do {
-                                try self.context.save()
-                                debugPrint(
-                                    "Pump History storage: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved smbs to core data"
-                                )
-                            } catch {
-                                debugPrint(
-                                    "Pump History storage: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save smbs to core data"
-                                )
-                            }
+                        do {
+                            try CoreDataStack.shared.backgroundContext.saveContext()
+                        } catch {
+                            print(error.localizedDescription)
                         }
                     }
 
@@ -108,17 +101,10 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
                         newTempBasal.rate = rate as NSDecimalNumber
                         newTempBasal.tempType = TempType.absolute.rawValue
 
-                        if self.context.hasChanges {
-                            do {
-                                try self.context.save()
-                                debugPrint(
-                                    "Pump History storage: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved temp basal to core data"
-                                )
-                            } catch {
-                                debugPrint(
-                                    "Pump History storage: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save temp basal to core data"
-                                )
-                            }
+                        do {
+                            try CoreDataStack.shared.backgroundContext.saveContext()
+                        } catch {
+                            print(error.localizedDescription)
                         }
                     }
 
@@ -154,17 +140,10 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
                         newPumpEvent.timestamp = event.date
                         newPumpEvent.type = PumpEvent.pumpSuspend.rawValue
 
-                        if self.context.hasChanges {
-                            do {
-                                try self.context.save()
-                                debugPrint(
-                                    "Pump History storage: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved suspension to core data"
-                                )
-                            } catch {
-                                debugPrint(
-                                    "Pump History storage: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save suspension to core data"
-                                )
-                            }
+                        do {
+                            try CoreDataStack.shared.backgroundContext.saveContext()
+                        } catch {
+                            print(error.localizedDescription)
                         }
                     }
                     return [
@@ -188,17 +167,10 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
                         newPumpEvent.timestamp = event.date
                         newPumpEvent.type = PumpEvent.pumpResume.rawValue
 
-                        if self.context.hasChanges {
-                            do {
-                                try self.context.save()
-                                debugPrint(
-                                    "Pump History storage: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved pump resumption to core data"
-                                )
-                            } catch {
-                                debugPrint(
-                                    "Pump History storage: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save pump resumption to core data"
-                                )
-                            }
+                        do {
+                            try CoreDataStack.shared.backgroundContext.saveContext()
+                        } catch {
+                            print(error.localizedDescription)
                         }
                     }
                     return [

+ 28 - 14
FreeAPS/Sources/Modules/AddTempTarget/AddTempTargetStateModel.swift

@@ -46,8 +46,10 @@ extension AddTempTarget {
                     saveToCoreData.duration = duration as NSDecimalNumber
                     saveToCoreData.startDate = Date()
 
-                    if self.coredataContext.hasChanges {
-                        try? self.coredataContext.save()
+                    do {
+                        try CoreDataStack.shared.backgroundContext.saveContext()
+                    } catch {
+                        print(error.localizedDescription)
                     }
                 }
                 saveSettings = true
@@ -56,8 +58,10 @@ extension AddTempTarget {
                     let saveToCoreData = TempTargets(context: coredataContext)
                     saveToCoreData.active = false
                     saveToCoreData.date = Date()
-                    if self.coredataContext.hasChanges {
-                        try? coredataContext.save()
+                    do {
+                        try CoreDataStack.shared.backgroundContext.saveContext()
+                    } catch {
+                        print(error.localizedDescription)
                     }
                 }
             }
@@ -89,15 +93,19 @@ extension AddTempTarget {
                 let saveToCoreData = TempTargets(context: self.coredataContext)
                 saveToCoreData.active = false
                 saveToCoreData.date = Date()
-                if self.coredataContext.hasChanges {
-                    try? self.coredataContext.save()
+                do {
+                    try CoreDataStack.shared.backgroundContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
 
                 let setHBT = TempTargetsSlider(context: self.coredataContext)
                 setHBT.enabled = false
                 setHBT.date = Date()
-                if self.coredataContext.hasChanges {
-                    try? self.coredataContext.save()
+                do {
+                    try CoreDataStack.shared.backgroundContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
             }
         }
@@ -142,8 +150,10 @@ extension AddTempTarget {
                     saveToCoreData.hbt = hbt
                     saveToCoreData.date = Date()
                     saveToCoreData.duration = duration as NSDecimalNumber
-                    if self.coredataContext.hasChanges {
-                        try? self.coredataContext.save()
+                    do {
+                        try CoreDataStack.shared.backgroundContext.saveContext()
+                    } catch {
+                        print(error.localizedDescription)
                     }
                 }
             }
@@ -173,15 +183,19 @@ extension AddTempTarget {
                         saveToCoreData.startDate = Date()
                         saveToCoreData.duration = whichID?.duration ?? 0
 
-                        if self.coredataContext.hasChanges {
-                            try? self.coredataContext.save()
+                        do {
+                            try CoreDataStack.shared.backgroundContext.saveContext()
+                        } catch {
+                            print(error.localizedDescription)
                         }
                     } else {
                         let saveToCoreData = TempTargets(context: self.coredataContext)
                         saveToCoreData.active = false
                         saveToCoreData.date = Date()
-                        if self.coredataContext.hasChanges {
-                            try? self.coredataContext.save()
+                        do {
+                            try CoreDataStack.shared.backgroundContext.saveContext()
+                        } catch {
+                            print(error.localizedDescription)
                         }
                     }
                 }

+ 25 - 37
FreeAPS/Sources/Modules/Bolus/BolusStateModel.swift

@@ -359,31 +359,24 @@ extension Bolus {
         }
 
         private func savePumpInsulin(amount _: Decimal) {
-            // create pump event
-            let newPumpEvent = PumpEventStored(context: context)
-            newPumpEvent.id = UUID().uuidString
-            newPumpEvent.timestamp = Date()
-            newPumpEvent.type = PumpEvent.bolus.rawValue
-
-            // create bolus entry and specify relationship to pump event
-            let newBolusEntry = BolusStored(context: context)
-            newBolusEntry.pumpEvent = newPumpEvent
-            newBolusEntry.amount = amount as NSDecimalNumber
-            newBolusEntry.isExternal = false
-            newBolusEntry.isSMB = false
-
             context.perform {
-                if self.context.hasChanges {
-                    do {
-                        try self.context.save()
-                        debugPrint(
-                            "Bolus State: \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved pump insulin to core data"
-                        )
-                    } catch {
-                        debugPrint(
-                            "Bolus State: \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save pump insulin to core data"
-                        )
-                    }
+                // create pump event
+                let newPumpEvent = PumpEventStored(context: self.context)
+                newPumpEvent.id = UUID().uuidString
+                newPumpEvent.timestamp = Date()
+                newPumpEvent.type = PumpEvent.bolus.rawValue
+
+                // create bolus entry and specify relationship to pump event
+                let newBolusEntry = BolusStored(context: self.context)
+                newBolusEntry.pumpEvent = newPumpEvent
+                newBolusEntry.amount = self.amount as NSDecimalNumber
+                newBolusEntry.isExternal = false
+                newBolusEntry.isSMB = false
+
+                do {
+                    try CoreDataStack.shared.viewContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
             }
         }
@@ -450,17 +443,10 @@ extension Bolus {
                 newBolusEntry.isExternal = true
                 newBolusEntry.isSMB = false
 
-                if self.context.hasChanges {
-                    do {
-                        try self.context.save()
-                        debugPrint(
-                            "Bolus State: \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved carbs to core data"
-                        )
-                    } catch {
-                        debugPrint(
-                            "Bolus State: \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save carbs to core data"
-                        )
-                    }
+                do {
+                    try CoreDataStack.shared.viewContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
             }
 
@@ -504,8 +490,10 @@ extension Bolus {
             if selection != nil {
                 try? context.delete(selection!)
 
-                if context.hasChanges {
-                    try? context.save()
+                do {
+                    try CoreDataStack.shared.viewContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
                 carbs = 0
                 fat = 0

+ 8 - 13
FreeAPS/Sources/Modules/DataTable/DataTableStateModel.swift

@@ -172,22 +172,17 @@ extension DataTable {
             saveToHealth.append(saveToJSON)
 
             // save to core data
-            let newItem = GlucoseStored(context: coredataContext)
-            newItem.id = UUID()
-            newItem.date = Date()
-            newItem.glucose = Int16(glucoseAsInt)
-            newItem.isManual = true
+            coredataContext.perform {
+                let newItem = GlucoseStored(context: self.coredataContext)
+                newItem.id = UUID()
+                newItem.date = Date()
+                newItem.glucose = Int16(glucoseAsInt)
+                newItem.isManual = true
 
-            if coredataContext.hasChanges {
                 do {
-                    try coredataContext.save()
-                    debugPrint(
-                        "Data table state model: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) added manual glucose to core data"
-                    )
+                    try CoreDataStack.shared.viewContext.saveContext()
                 } catch {
-                    debugPrint(
-                        "Data table state model: \(#function) \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to add manual glucose to core data"
-                    )
+                    print(error.localizedDescription)
                 }
             }
         }

+ 5 - 2
FreeAPS/Sources/Modules/Home/HomeStateModel.swift

@@ -265,8 +265,11 @@ extension Home {
                 let profiles = Override(context: self.context)
                 profiles.enabled = false
                 profiles.date = Date()
-                if self.context.hasChanges {
-                    try? self.context.save()
+
+                do {
+                    try CoreDataStack.shared.backgroundContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
             }
         }

+ 5 - 2
FreeAPS/Sources/Modules/NightscoutConfig/NightscoutConfigStateModel.swift

@@ -316,8 +316,11 @@ extension NightscoutConfig {
                 let saveToCoreData = ImportError(context: self.coredataContext)
                 saveToCoreData.date = Date()
                 saveToCoreData.error = string
-                if coredataContext.hasChanges {
-                    try? coredataContext.save()
+
+                do {
+                    try CoreDataStack.shared.backgroundContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
             }
         }

+ 40 - 20
FreeAPS/Sources/Modules/OverrideProfilesConfig/OverrideProfilesStateModel.swift

@@ -95,8 +95,10 @@ extension OverrideProfilesConfig {
                     saveOverride.smbMinutes = smbMinutes as NSDecimalNumber
                     saveOverride.uamMinutes = uamMinutes as NSDecimalNumber
                 }
-                if self.coredataContext.hasChanges {
-                    try? self.coredataContext.save()
+                do {
+                    try CoreDataStack.shared.backgroundContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
             }
         }
@@ -138,8 +140,10 @@ extension OverrideProfilesConfig {
                     saveOverride.smbMinutes = smbMinutes as NSDecimalNumber
                     saveOverride.uamMinutes = uamMinutes as NSDecimalNumber
                 }
-                if self.coredataContext.hasChanges {
-                    try? self.coredataContext.save()
+                do {
+                    try CoreDataStack.shared.backgroundContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
             }
         }
@@ -180,8 +184,10 @@ extension OverrideProfilesConfig {
                     saveOverride.smbMinutes = (profile.smbMinutes ?? 0) as NSDecimalNumber
                     saveOverride.uamMinutes = (profile.uamMinutes ?? 0) as NSDecimalNumber
                 }
-                if self.coredataContext.hasChanges {
-                    try? self.coredataContext.save()
+                do {
+                    try CoreDataStack.shared.backgroundContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
             }
         }
@@ -268,8 +274,10 @@ extension OverrideProfilesConfig {
                 let profiles = Override(context: self.coredataContext)
                 profiles.enabled = false
                 profiles.date = Date()
-                if self.coredataContext.hasChanges {
-                    try? self.coredataContext.save()
+                do {
+                    try CoreDataStack.shared.backgroundContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
             }
             smbMinutes = defaultSmbMinutes
@@ -302,8 +310,10 @@ extension OverrideProfilesConfig {
                     let saveToCoreData = TempTargets(context: coredataContext)
                     saveToCoreData.active = false
                     saveToCoreData.date = Date()
-                    if self.coredataContext.hasChanges {
-                        try? coredataContext.save()
+                    do {
+                        try CoreDataStack.shared.backgroundContext.saveContext()
+                    } catch {
+                        print(error.localizedDescription)
                     }
                 }
             }
@@ -335,15 +345,19 @@ extension OverrideProfilesConfig {
                 let saveToCoreData = TempTargets(context: self.coredataContext)
                 saveToCoreData.active = false
                 saveToCoreData.date = Date()
-                if self.coredataContext.hasChanges {
-                    try? self.coredataContext.save()
+                do {
+                    try CoreDataStack.shared.backgroundContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
 
                 let setHBT = TempTargetsSlider(context: self.coredataContext)
                 setHBT.enabled = false
                 setHBT.date = Date()
-                if self.coredataContext.hasChanges {
-                    try? self.coredataContext.save()
+                do {
+                    try CoreDataStack.shared.backgroundContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
             }
         }
@@ -388,8 +402,10 @@ extension OverrideProfilesConfig {
                     saveToCoreData.hbt = hbt
                     saveToCoreData.date = Date()
                     saveToCoreData.duration = durationTT as NSDecimalNumber
-                    if self.coredataContext.hasChanges {
-                        try? self.coredataContext.save()
+                    do {
+                        try CoreDataStack.shared.backgroundContext.saveContext()
+                    } catch {
+                        print(error.localizedDescription)
                     }
                 }
             }
@@ -419,15 +435,19 @@ extension OverrideProfilesConfig {
                         saveToCoreData.startDate = Date()
                         saveToCoreData.duration = whichID?.duration ?? 0
 
-                        if self.coredataContext.hasChanges {
-                            try? self.coredataContext.save()
+                        do {
+                            try CoreDataStack.shared.backgroundContext.saveContext()
+                        } catch {
+                            print(error.localizedDescription)
                         }
                     } else {
                         let saveToCoreData = TempTargets(context: self.coredataContext)
                         saveToCoreData.active = false
                         saveToCoreData.date = Date()
-                        if self.coredataContext.hasChanges {
-                            try? self.coredataContext.save()
+                        do {
+                            try CoreDataStack.shared.backgroundContext.saveContext()
+                        } catch {
+                            print(error.localizedDescription)
                         }
                     }
                 }

+ 0 - 1
FreeAPS/Sources/Services/WatchManager/WatchManager.swift

@@ -61,7 +61,6 @@ final class BaseWatchManager: NSObject, WatchManager, Injectable {
             key: "deliverAt",
             ascending: false,
             fetchLimit: 1,
-            batchSize: 1,
             propertiesToFetch: ["deliverAt"]
         ).first?.deliverAt
     }

+ 8 - 4
FreeAPS/Sources/Shortcuts/TempPresets/TempPresetsIntentRequest.swift

@@ -56,15 +56,19 @@ import Foundation
                 saveToCoreData.startDate = Date()
                 saveToCoreData.duration = whichID?.duration ?? 0
 
-                if coredataContext.hasChanges {
-                    try? self.coredataContext.save()
+                do {
+                    try CoreDataStack.shared.backgroundContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
             } else {
                 let saveToCoreData = TempTargets(context: self.coredataContext)
                 saveToCoreData.active = false
                 saveToCoreData.date = Date()
-                if coredataContext.hasChanges {
-                    try? self.coredataContext.save()
+                do {
+                    try CoreDataStack.shared.backgroundContext.saveContext()
+                } catch {
+                    print(error.localizedDescription)
                 }
             }
         }

+ 18 - 0
Model/CoreDataStack.swift

@@ -77,3 +77,21 @@ class CoreDataStack: ObservableObject {
         return result ?? []
     }
 }
+
+extension NSManagedObjectContext {
+    func saveContext(callingFunction: String = #function, callingClass: String = #fileID) throws {
+        if hasChanges {
+            do {
+                try save()
+                debugPrint(
+                    "Saving to Core Data successful in \(callingFunction) in \(callingClass): \(DebuggingIdentifiers.succeeded)"
+                )
+            } catch let error as NSError {
+                debugPrint(
+                    "Saving to Core Data failed in \(callingFunction) in \(callingClass): \(DebuggingIdentifiers.failed) with error \(error), \(error.userInfo)"
+                )
+                throw error
+            }
+        }
+    }
+}