Fetching symptoms from an HKElectrocardiogram

Viewed 227

I am trying to retrieve all symptoms from a HKElectrocardiogram with the code below:

    func getAllSymptoms(from sample: HKElectrocardiogram) {
        let catIds: [HKCategoryTypeIdentifier] =  [
            .rapidPoundingOrFlutteringHeartbeat,
            .skippedHeartbeat,
            .fatigue,
            .shortnessOfBreath,
            .chestTightnessOrPain,
            .fainting,
            .dizziness,
        ]
        for catId in catIds {
            getSymptoms(from: sample, categoryType: catId) {
                (cat: HKCategoryTypeIdentifier, userEntered: Bool) in
                print("\(cat) entered: \(userEntered)")
            }
        }
    }
    
    func getSymptoms(from sample: HKElectrocardiogram,
                         categoryType: HKCategoryTypeIdentifier,
                         completion: @escaping (HKCategoryTypeIdentifier, Bool)->Void){
        guard sample.symptomsStatus == .present,
              let sampleType = HKSampleType.categoryType(forIdentifier: categoryType) else {
            completion(categoryType, false)
            return
        }
        let predicate = HKQuery.predicateForObjectsAssociated(electrocardiogram: sample)
        let sampleQuery = HKSampleQuery(
            sampleType: sampleType,
            predicate: predicate,
            limit: HKObjectQueryNoLimit,
            sortDescriptors: nil) { (query, samples, error) in
            if let sample = samples?.first,
               let categorySample = sample as? HKCategorySample,
               let userEntered = categorySample.metadata?["HKWasUserEntered"] {
                completion(categoryType, userEntered as! Int == 1)
            } else {
                completion(categoryType, false)
            }
        }
        
        healthStore.execute(sampleQuery)
    }

Does anyone know:

  1. The code does what it suppose to do. But, because the code is querying one category at a time, I need to execute 7 queries in order to get all symptoms for an HKElectrocardiogram. I am wondering whether it's possible to have the same result by executing one query?
  2. There is an "Other" option in "Add Symptoms" after recording an ECG, which HKCategoryTypeIdentifier should I use to know whether that is included?

Thanks

0 Answers
Related