Просмотр исходного кода

Fix fetchEntities() by providing proper resultType

Deniz Cengiz 1 год назад
Родитель
Сommit
eba51af9c8

+ 1 - 1
FreeAPS/Sources/APS/FetchGlucoseManager.swift

@@ -198,7 +198,7 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable {
             key: "date",
             ascending: false,
             fetchLimit: 6
-        )
+        ) as? [GlucoseStored]
     }
 
     private func processGlucose() -> [BloodGlucose] {

+ 2 - 2
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -915,7 +915,7 @@ extension OpenAPS {
             key: "date",
             ascending: false,
             fetchLimit: 1
-        )
+        ) as? [TempTargetStored] ?? []
     }
 
     func fetchActiveOverrides() -> [OverrideStored] {
@@ -926,7 +926,7 @@ extension OpenAPS {
             key: "date",
             ascending: false,
             fetchLimit: 1
-        )
+        ) as? [OverrideStored] ?? []
     }
 
     func fetchHistoricalTDDData(from date: Date) -> [[String: Any]] {

+ 2 - 2
FreeAPS/Sources/APS/Storage/GlucoseStorage.swift

@@ -264,14 +264,14 @@ final class BaseGlucoseStorage: GlucoseStorage, Injectable {
 
     func fetchLatestGlucose() -> GlucoseStored? {
         let predicate = NSPredicate.predicateFor20MinAgo
-        return CoreDataStack.shared.fetchEntities(
+        return (CoreDataStack.shared.fetchEntities(
             ofType: GlucoseStored.self,
             onContext: coredataContext,
             predicate: predicate,
             key: "date",
             ascending: false,
             fetchLimit: 1
-        ).first
+        ) as? [GlucoseStored] ?? []).first
     }
 
     // Fetch glucose that is not uploaded to Nightscout yet

+ 1 - 1
FreeAPS/Sources/APS/Storage/PumpHistoryStorage.swift

@@ -60,7 +60,7 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
                         key: "timestamp",
                         ascending: false,
                         batchSize: 50
-                    )
+                    ) as? [PumpEventStored] ?? []
 
                     switch event.type {
                     case .bolus:

+ 3 - 2
FreeAPS/Sources/Shortcuts/State/StateIntentRequest.swift

@@ -68,7 +68,7 @@ final class StateIntentRequest: BaseIntentsRequest {
                 key: "date",
                 ascending: false,
                 fetchLimit: 2
-            )
+            ) as? [GlucoseStored] ?? []
 
             guard let lastValue = results.first else { throw StateIntentError.NoBG }
 
@@ -109,7 +109,8 @@ final class StateIntentRequest: BaseIntentsRequest {
             key: "deliverAt",
             ascending: false,
             fetchLimit: 1
-        )
+        ) as? [OrefDetermination] ?? []
+
         let iobAsDouble = Double(truncating: (results.first?.iob ?? 0.0) as NSNumber)
         let cobAsDouble = Double(truncating: (results.first?.cob ?? 0) as NSNumber)
 

+ 13 - 17
Model/CoreDataStack.swift

@@ -313,8 +313,8 @@ extension CoreDataStack {
         propertiesToFetch: [String]? = nil,
         callingFunction: String = #function,
         callingClass: String = #fileID
-    ) -> [T] {
-        let request = NSFetchRequest<T>(entityName: String(describing: type))
+    ) -> [Any] {
+        let request = NSFetchRequest<NSFetchRequestResult>(entityName: String(describing: type))
         request.sortDescriptors = [NSSortDescriptor(key: key, ascending: ascending)]
         request.predicate = predicate
         if let limit = fetchLimit {
@@ -323,9 +323,9 @@ extension CoreDataStack {
         if let batchSize = batchSize {
             request.fetchBatchSize = batchSize
         }
-        if let propertiesTofetch = propertiesToFetch {
-            request.propertiesToFetch = propertiesTofetch
-            request.resultType = .managedObjectResultType
+        if let propertiesToFetch = propertiesToFetch {
+            request.propertiesToFetch = propertiesToFetch
+            request.resultType = .dictionaryResultType
         } else {
             request.resultType = .managedObjectResultType
         }
@@ -333,23 +333,22 @@ extension CoreDataStack {
         context.name = "fetchContext"
         context.transactionAuthor = "fetchEntities"
 
-        var result: [T]?
-
         /// we need to ensure that the fetch immediately returns a value as long as the whole app does not use the async await pattern, otherwise we could perform this asynchronously with backgroundContext.perform and not block the thread
-        context.performAndWait {
+        return context.performAndWait {
             do {
-//                debugPrint(
-//                    "Fetching \(T.self) in \(callingFunction) from \(callingClass): \(DebuggingIdentifiers.succeeded) on Thread: \(Thread.current)"
-//                )
-                result = try context.fetch(request)
+                if propertiesToFetch != nil {
+                    return try context.fetch(request) as? [[String: Any]] ?? []
+                } else {
+                    return try context.fetch(request) as? [T] ?? []
+                }
             } catch let error as NSError {
                 debugPrint(
                     "Fetching \(T.self) in \(callingFunction) from \(callingClass): \(DebuggingIdentifiers.failed) \(error) on Thread: \(Thread.current)"
                 )
+
+                return []
             }
         }
-
-        return result ?? []
     }
 
     // Fetch Async
@@ -386,9 +385,6 @@ extension CoreDataStack {
 
         return await context.perform {
             do {
-//                debugPrint(
-//                    "Fetching \(T.self) in \(callingFunction) from \(callingClass): \(DebuggingIdentifiers.succeeded) on Thread: \(Thread.current)"
-//                )
                 if propertiesToFetch != nil {
                     return try context.fetch(request) as? [[String: Any]] ?? []
                 } else {