2020 Health & Fitness
WWDC20 · 8 min · Health & Fitness
Handling FHIR without getting burned
Learn how FHIRModels creates native data models for all FHIR resources, provides data validation to enforce resource integrity, and prevents the creation of structurally invalid resources — across multiple versions of the FHIR specification. Whether you’re working with clinical data obtained from HealthKit or direct from a clinical system, FHIRModels makes FHIR easy to handle.
Watch at developer.apple.com ↗Code shown on screen · 3 snippets
Use FHIRModels with Health Records FHIR data from HealthKit
// Use with Health Records FHIR data from HealthKit
import HealthKit
import ModelsDSTU2
// Grab HKClinicalRecord from HealthKit API
let clinicalRecord: HKClinicalRecord
let resource = clinicalRecord.fhirResource!
// Print the prescription note
let decoder = JSONDecoder()
let prescription = try decoder.decode(MedicationOrder.self, from: resource.data)
print("\(prescription.note)") Use FHIRModels with Health Records FHIR data from HealthKit, part 2
// Make using "TimingRepeat" period dates easier by writing an extension
extension TimingRepeat {
var periodDisplayString: String? {
if case .period(let period) = bounds {
return "\(period.start) - \(period.end)"
}
return nil
}
}
// Collect all dosage instructions on medication prescriptions
let instructions: [String] = prescription.dosageInstruction?.map { dosage in
guard let period = dosage.timing?.repeat?.periodDisplayString else {
return "\(dosage.text)"
}
return "\(period): \(dosage.text)"
} Supporting multiple FHIR releases
// Supporting multiple releases
import ModelsDSTU2
import ModelsR4
let decoder = JSONDecoder()
let release: FHIRRelease
let data: Data
let note: String? = nil
switch release {
case .dstu2:
let model = try decoder.decode(ModelsDSTU2.MedicationOrder.self, from: data)
note = model.note?.value?.string
case .r4:
let model = try decoder.decode(ModelsR4.MedicationRequest.self, from: data)
note = model.note?.compactMap({ $0.text.value?.string }).joined(separator: "\n")
default:
note = "Unsupported FHIR release \(release)"
} Resources
Related sessions
-
18 min -
36 min -
6 min -
22 min -
38 min