【问题标题】:Problems returning heart rate values using `HKAnchoredObjectQuery` on Series 5 Watch and Watch OS6在 Series 5 Watch 和 Watch OS6 上使用“HKAnchoredObjectQuery”返回心率值时出现问题
【发布时间】:2019-10-28 11:13:59
【问题描述】:

我使用HKAnchoredObjectQuery 来查询心率,因为我不想为此应用使用 WorkoutBuilder API。此查询始终以正常采样率(每秒等)在锻炼期间返回心率,但我注意到在 Series 5 Watch OS 6.1 上进行测试时,我只看到非常零星的样本,例如一小时内测了 5 次心率。 HKAnchoredObjectQuery 的文档中没有任何内容表明它已被弃用。知道为什么这种心率收集方法不再有效吗?

    func startHeartRateQuery(from startDate: Date, updateHandler: @escaping ([HKQuantitySample]) -> Void) {
        let typeIdentifier = HKQuantityTypeIdentifier.heartRate
        startQuery(ofType: typeIdentifier, from: startDate) { _, samples, _, _, error in
            guard let quantitySamples = samples as? [HKQuantitySample] else {
                print("Heart rate query failed with error: \(String(describing: error))")
                return
            }
            updateHandler(quantitySamples)

        }
    }


   //Generic helper function 
    private func startQuery(ofType type: HKQuantityTypeIdentifier, from startDate: Date, handler: @escaping
        (HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void) {
        let datePredicate = HKQuery.predicateForSamples(withStart: startDate, end: nil, options: .strictStartDate)
        let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
        let queryPredicate = NSCompoundPredicate(andPredicateWithSubpredicates:[datePredicate, devicePredicate])

        let quantityType = HKObjectType.quantityType(forIdentifier: type)!

        let query = HKAnchoredObjectQuery(type: quantityType, predicate: queryPredicate, anchor: nil,
                                          limit: HKObjectQueryNoLimit, resultsHandler: handler)
        query.updateHandler = handler
        healthStore.execute(query)

        activeDataQueries.append(query)
    }

【问题讨论】:

    标签: swift apple-watch healthkit watchos heartrate


    【解决方案1】:

    奇怪,但事实证明我在 Watch 应用程序的设置中设置了锻炼省电模式。除非我意外设置它在我安装 Watch OS 6 时设置为开启?将其关闭后,心率查询再次正常返回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-12
      • 2015-08-26
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多