ios 从HK心电图中提取症状

aurhwmvo  于 2023-07-01  发布在  iOS
关注(0)|答案(1)|浏览(95)

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

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)
    }

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

相关问题