DataManager.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. import Foundation
  2. // Fetch Data for Glucose and Determination from Core Data and map them to the Structs in order to pass them thread safe to the glucoseDidUpdate/ pushUpdate function
  3. @available(iOS 16.2, *)
  4. extension LiveActivityBridge {
  5. func fetchAndMapGlucose() async {
  6. await context.perform {
  7. self.glucoseFromPersistence = CoreDataStack.shared.fetchEntities(
  8. ofType: GlucoseStored.self,
  9. onContext: self.context,
  10. predicate: NSPredicate.predicateForSixHoursAgo,
  11. key: "date",
  12. ascending: false,
  13. fetchLimit: 72
  14. ).map { GlucoseData(glucose: Int($0.glucose), date: $0.date ?? Date(), direction: $0.directionEnum) }
  15. }
  16. }
  17. func fetchAndMapDetermination() async {
  18. await context.perform {
  19. self.determination = CoreDataStack.shared.fetchEntities(
  20. ofType: OrefDetermination.self,
  21. onContext: self.context,
  22. predicate: NSPredicate.enactedDetermination,
  23. key: "deliverAt",
  24. ascending: false,
  25. fetchLimit: 1,
  26. propertiesToFetch: ["iob", "cob", "deliverAt"]
  27. ).first.map { DeterminationData(cob: Int($0.cob), iob: $0.iob?.decimalValue ?? 0) }
  28. }
  29. }
  30. }