从 HK 心电图中获取症状

cmh*_*cmh 5 ios swift healthkit apple-watch

我正在尝试使用以下代码从香港心电图中检索所有症状:

    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)
    }
Run Code Online (Sandbox Code Playgroud)

有人知道吗:

  1. 代码做了它应该做的事情。但是,由于代码一次查询一个类别,因此我需要执行 7 个查询才能获取 HK 心电图的所有症状。我想知道是否可以通过执行一个查询来获得相同的结果?
  2. 记录心电图后,“添加症状”中有一个“其他”选项,我应该使用哪个 HKCategoryTypeIdentifier 来知道是否包含该选项?

谢谢