Jelajahi Sumber

fix Smoothing using oldest 350 values of last 24hrs

The fetch uses ascending: true (oldest first) combined with fetchLimit: 350. Core Data applies sort + limit at the SQL level, so it returns the oldest 350 rows that match the predicate — not the newest. This changes it to newest 350 values of the day.
Robert 3 minggu lalu
induk
melakukan
e2517ee25d
1 mengubah file dengan 4 tambahan dan 2 penghapusan
  1. 4 2
      Trio/Sources/APS/FetchGlucoseManager.swift

+ 4 - 2
Trio/Sources/APS/FetchGlucoseManager.swift

@@ -381,9 +381,11 @@ extension BaseFetchGlucoseManager {
             // Predicate must cover at least the full glucose horizon used by downstream algorithm consumers.
             // If autosens / oref / smoothing logic ever starts looking back further (e.g. 36h),
             // this fetch window must be expanded accordingly.
+            // Fetch descending (newest first) so the limit always keeps the most recent 350 readings.
+            // Reversed before return so callers receive oldest-first (chronological) order.
             predicate: compoundPredicate,
             key: "date",
-            ascending: true, // the first element is the oldest
+            ascending: false,
             fetchLimit: 350
         )
 
@@ -391,7 +393,7 @@ extension BaseFetchGlucoseManager {
             throw CoreDataError.fetchError(function: #function, file: #file)
         }
 
-        return glucoseArray.map(\.objectID)
+        return Array(glucoseArray.map(\.objectID).reversed())
     }
 
     /// CoreData-friendly AAPS exponential smoothing + storage.