Post

Replies

Boosts

Views

Activity

Reply to SwiftData & CloudKit: Arrays of Codable Structs Causing NSKeyedUnarchiveFromData Error
I switch to using Data with Transient computed property and it still is showing the same error logged to the console: import SwiftData @Model final class ProtocolMedication { var uuid: UUID = UUID() var createdAt: Date = Date() var frequency: SchedulingFrequency = SchedulingFrequency.atRegularIntervals var interval: SchedulingInterval = SchedulingInterval(days: 1) var startDate: Date = Date() var timesData: Data? var dayOfWeekSelection: DayOfWeekSelection = DayOfWeekSelection(days: [1]) var injectionRotationConfig: InjectionRotationConfig = InjectionRotationConfig() var medicationConcentration: MedicationConcentration = MedicationConcentration(value: nil, unit: nil) var medication: Medication? @Relationship(deleteRule: .cascade, inverse: \ScheduledDose.protocolMed) var _scheduledDoses: [ScheduledDose]? @Relationship(deleteRule: .cascade, inverse: \DoseLog.protocolMed) var _doseLogs: [DoseLog]? @Transient var times: [SchedulingTime] { get { guard let data = timesData else { return [] } return (try? PropertyListDecoder().decode([SchedulingTime].self, from: data)) ?? [] } set { timesData = try? PropertyListEncoder().encode(newValue) } } var scheduledDoses: [ScheduledDose] { _scheduledDoses ?? [] } var doseLogs: [DoseLog] { _doseLogs ?? [] } init(frequency: SchedulingFrequency, interval: SchedulingInterval = SchedulingInterval(days: 1), startDate: Date, times: [SchedulingTime] = [], dayOfWeekSelection: DayOfWeekSelection = DayOfWeekSelection(days: [1]), injectionRotationConfig: InjectionRotationConfig = InjectionRotationConfig(), medicationConcentration: MedicationConcentration, medication: Medication) { self.frequency = frequency self.interval = interval self.startDate = startDate self.times = times self.dayOfWeekSelection = dayOfWeekSelection self.injectionRotationConfig = injectionRotationConfig self.medicationConcentration = medicationConcentration self.medication = medication } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
2d
Reply to Should ModelActor be used to populate a view?
I decided to run this task on app launch to sync the clinical labs into the SwiftData store. This allows me to use Query macro in the views where it's needed and no need to combine lab results. Let me know if this is the right approach: import SwiftData import SwiftUI import OSLog @ModelActor actor HealthKitSyncManager { private let logger = Logger(subsystem: "com.trtmanager", category: "HealthKitSyncManager") func sync() async { do { // Get all Clinical labs let hkClinicalRecords = try await TRTHealthKitService.getHKClinicalRecords() guard !hkClinicalRecords.isEmpty else { return } // Get existing HealthKit IDs in one query let descriptor = FetchDescriptor<LabResult>( // predicate: #Predicate { $0.source == .clinicalRecord } ) let existing = try modelContext.fetch(descriptor) .compactMap { $0.fhirResourceId } let existingIDs = Set(existing) let missingRecords = hkClinicalRecords.filter { lab in guard let fhirResource = lab.fhirResource else { return false } return !existingIDs.contains(fhirResource.identifier) } var labResults: [LabResult] = [] // Insert only new ones for record in missingRecords { if let fhirResource = record.fhirResource { do { let parsedResults = try TRTHealthKitService.parseFHIRLabResults(fhirResource) labResults.append(contentsOf: parsedResults) } catch { logger.warning("Skipping bad record: \(error.localizedDescription)") } } } for labResult in labResults { modelContext.insert(labResult) } try modelContext.save() logger.info("Successfully synced \(labResults.count) lab results") } catch { logger.error("Sync failed") } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
2w
Reply to Question about BGAppRefreshTask approach for medication scheduling app
@DTS Engineer After reading your post on iOS Background Execution Limits it sounds like I can't rely on BGAppRefreshTaskRequest to ensure a scheduled dose window of 2 weeks for my users. That is because "... there are common scenarios where it won’t grant you any background execution time at all!". How does Apple's Medications app ensure this 2 week window along with Local Notifications? Do they have access to an api that I don't to make it possible? I could always create an idempotent function that ensures the user's dosing schedule is generated when the app is loaded. But if the user doesn't open the app for awhile, Local Notifications will be exhausted and the user wont receive any more notifications until the app loads and updates the schedule. Do you understand my concern?
Apr ’25