【问题标题】:How can I retrieve the heart beats during a period of time using HKHeartbeatSeriesSample?如何使用 HKHeartbeatSeriesSample 检索一段时间内的心跳?
【发布时间】:2020-11-10 10:31:49
【问题描述】:

我正在尝试根据WWDC 2019 - Exploring new Data Representations in HealthKit 的这次会议从 healthStore 获取基础心跳数据。在标记 24:40 解释了 HealthKit 如何拥有一个能够存储准确时间戳的传感器,并且可以通过 HKHeartbeatSeriesSample 类型访问这些数据。

我创建了这个查询来检索该数据:

func queryData(start: Date, end: Date) {
    let predicate = HKQuery.predicateForSamples(withStart: start, end: end, options: [])
    
    let heartBeatSeriesSample = HKSampleQuery(sampleType: HKSeriesType.heartbeat(), predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) {
        (query, results, error) in
        
        guard let samples = results, let sample = samples.first as? HKHeartbeatSeriesSample else {
            print("Failed: no samples collected")
            return
        }
        print(results as Any)
        
        let heartBeatSeriesQuery = HKHeartbeatSeriesQuery(heartbeatSeries: sample) {
            (query, timeSinceSeriesStart, precededByGap, done, error) in
            guard error == nil else {
                print("Failed querying the raw heartbeat data: \(String(describing: error))")
                return
            }
            print(timeSinceSeriesStart)
        }
        self.healthStore?.execute(heartBeatSeriesQuery)
    }
    healthStore?.execute(heartBeatSeriesSample)
}

但它只有在我不使用谓词时才有效,它会显示之前存储的所有心跳数据,但在此期间没有存储任何内容。 我已经设法通过以下方式使用HKHeartbeatSeriesBuilder 存储了一些数据:

let builder = HKHeartbeatSeriesBuilder(
    healthStore: self.healthStore!,
    device: .local(),
    start: start
)

builder.addHeartbeatWithTimeInterval(sinceSeriesStartDate: 0.5, precededByGap: false) {
    (success, error) in

    guard success else {
        fatalError("Could not add heartbeat: \(String(describing: error))")
    }
}

它存储的是数据,而不是 Apple Watch 设备读取的实际节拍。因为我需要实际的节拍来以这种方式存储它们,所以在我必须首先存储它之前,必须有一个查询给我这些数据(经典的鸡和蛋)。

如何在给定时间段内读取带有时间戳的准确心跳?

【问题讨论】:

  • 你找到答案了吗?

标签: ios swift healthkit watchos


【解决方案1】:

是的。不幸的是,仅使用 Apple Watch 无法提取原始数据。我在苹果开发者支持中打开了一张票,他们说广告功能是可能的,使用第三方设备。非常令人失望,而且以后肯定不会选择 Apple 来完成这样一个严肃的项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多