访问静息心率

Accessing resting heart rate

美好的一天,

我是 Swift 4 的新手,在获取静息心率数据时遇到问题。

这是我的资料:

// 声明 var heartRateType = HKAnchoredObjectQuery.self

private func createStreamingQuery() -> HKQuery {

    let calendar = NSCalendar.current;

    let now = NSDate();

    let sevenDaysAgo = calendar.date(byAdding: .day, value: -7, to: now as Date);

    let startDate = calendar.startOfDay(for: sevenDaysAgo!);

    let predicate = HKQuery.predicateForSamples(withStart: startDate as Date, end: now as Date, options: [])

    let query = HKAnchoredObjectQuery(type: heartRateType, predicate: predicate, anchor: nil, limit: Int(HKObjectQueryNoLimit)) {

        (query, samples, deletedObjects, anchor, error) -> Void in

        self.formatSamples(samples: samples)

    }

    query.updateHandler = { (query, samples, deletedObjects, anchor, error) -> Void in
        self.formatSamples(samples: samples)
    }

    return query
}

@IBAction func readHeartRate(_ sender: Any) {
    self.healthKitStore.execute(self.createStreamingQuery())
}

private func formatSamples(samples: [HKSample]?) {
    guard let heartRateSamples = samples as? [HKQuantitySample] else { return }

    guard let sample = heartRateSamples.first else{return}
    let value = sample.quantity.doubleValue(for: heartRateType)

    print("HeartRate: \(value)")
}

我在这些行中遇到以下错误,无法检查这是否有效。

// 错误:无法推断通用参数 'T'

// 错误代码:

    let query = HKAnchoredObjectQuery(type: heartRateType, predicate: predicate, anchor: nil, limit: Int(HKObjectQueryNoLimit)) {

        (query, samples, deletedObjects, anchor, error) -> Void in

        self.formatSamples(samples: samples)

    }

其他错误:无法将类型 'HKAnchoredObjectQuery.Type' 的值转换为预期的参数类型 'HKUnit'

// 错误代码:

    let value = sample.quantity.doubleValue(for: heartRateType)

非常感谢您提供的任何帮助。

非常感谢!

凯文

自从我发布这篇文章以来,我已经对此进行了研究和了解。我最终将函数重写为以下内容:

//MARK: - Get Users Resting Heart Rate
func getUsersRestingHeartRate(completion: @escaping (HKQuantitySample) -> Void) {

//        print("getUsersRestingHeartRate(Completion)")

    guard let restingHeartRateSampleType = HKSampleType.quantityType(forIdentifier: .restingHeartRate) else {
        print("Resting Heart Rate Sample Type is no longer available in HealthKit")
        return
    }

    //1. Use HKQuery to load the most recent samples.
    let mostRecentPredicate = HKQuery.predicateForSamples(withStart: Date.distantPast,
                                                          end: Date(),
                                                          options: .strictEndDate)
    let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)

    let sampleQuery = HKSampleQuery(sampleType: restingHeartRateSampleType,
                                    predicate: mostRecentPredicate,
                                    limit: HKObjectQueryNoLimit,
                                    sortDescriptors:
    [sortDescriptor]) { (query, samples, error) in
        DispatchQueue.main.async {
            guard let samples = samples,
                let mostRecentSample = samples.first as? HKQuantitySample else {
                    print("getUserRestingHeartRate sample is missing")
                    return
            }
            completion(mostRecentSample)
        }
    }
    HKHealthStore().execute(sampleQuery)
}