The Clinical Health Records - App Crash & Sample JSON Date type details

Hello,

With reference to HealthKit - Clinical Health Records, we implemented the following method to fetch the User's clinical records but seems the query is failing and the app is getting crashed every time.

We tried the same method with Apple provided sample account and it works fine in the simulator, but it crashes with actual users

Can you please help us to identify the issue?

    func getRecordsForCHRType(type: HKClinicalTypeIdentifier, completion:@escaping ([HKClinicalRecord]?) -> Void) {
    guard let healthRecordType = HKObjectType.clinicalType(forIdentifier: type) else { return }

    let startDate = Calendar.current.date(byAdding: DateComponents(day: -365), to: Date())!
    let endDate = Calendar.current.date(byAdding: DateComponents(day: 0), to: Date())!
    let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [.strictStartDate])
    let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
    let healthRecordQuery = HKSampleQuery(sampleType: healthRecordType, predicate: predicate, limit: 0, sortDescriptors: [sortDescriptor]) { (query, samples, error) in
        guard let actualSamples = samples else {
            completion(nil)
            return
        }
        let healthRecordSamples = actualSamples as? [HKClinicalRecord]
        completion(healthRecordSamples)
    }
    HKHealthStore().execute(healthRecordQuery)
   }

Additionally, please help us to understand the following different types of dates(in detail) available in sample Clinical Health Records JSON, and is it possible to apply a filter using any HealthKitQuery on these dates to narrow down the clinical records response?

  1. issued
  2. dateRecorded
  3. recordDate
  4. onset
  5. effective

Any help would be appreciated!

The crash should definitely not happen, can you paste the stack trace here?

Regarding dates, it depends a lot on which resource type you're parsing and also which dates are actually populated in the data model, which depends on data provenance. The "start date" and "end date" have a different meaning, see the documentation at https://developer.apple.com/documentation/healthkit/hkclinicalrecord:

Note that the record inherits the HKSample class’s startDate and endDate properties. However, the system does not populate these properties with information from the FHIR data; instead, the startDate and endDate reflect the time and date when the system downloaded the FHIR data to the device.

The Clinical Health Records - App Crash & Sample JSON Date type details
 
 
Q