获得静息心率

问题描述 投票:1回答:1

早安,

我是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)

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

非常感谢!

凯文

ios swift4 healthkit
1个回答
0
投票

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

//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)
}
© www.soinside.com 2019 - 2024. All rights reserved.