swift - hkanchoredObjectQuery UpdateHandler仅调用一次

标签 swift healthkit

我在 iPhone 应用程序中有 ViewController:

class ViewController: UIViewController {
    private let healthStore = HKHealthStore()
    private let heartRateUnit = HKUnit(fromString: "count/min")
    private var anchor = HKQueryAnchor(fromValue: Int(HKAnchoredObjectQueryNoAnchor))

    override func viewDidLoad() {
        super.viewDidLoad()

        if let query = createHeartRateStreamingQuery(NSDate()) {
            healthStore.executeQuery(query)
        }
    }

     func createHeartRateStreamingQuery(workoutStartDate: NSDate) -> HKQuery? {
        guard let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate) else { return .None }

        let heartRateQuery = HKAnchoredObjectQuery(type: quantityType, predicate: nil, anchor: anchor, limit: Int(HKObjectQueryNoLimit)) { (query, sampleObjects, deletedObjects, newAnchor, error)  in }

        heartRateQuery.updateHandler = {(query, samples, deleteObjects, newAnchor, error) in
            self.updateHeartRate(samples)
        }

        return heartRateQuery
    }

但是当我使用锻炼应用程序时,heartRateQuery.updateHandler 仅调用一次。

看起来是同样的错误 - https://forums.developer.apple.com/thread/14571Monitor heart rate from HealthKit --> HKAnchoredObjectQuery only called after applicationDidBecomeActive (BUG or FEATURE?)

最佳答案

您需要为您的样本类型(心率)启用后台传送。查找方法

HKHealthStore.enableBackgroundDeliveryForType()

我在之前执行此方法

HKHealthStore.executeQuery().

关于swift - hkanchoredObjectQuery UpdateHandler仅调用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34143643/

相关文章:

ios - 如何在 Modal View presentation swift iOS 中添加动画延迟

ios - UIScrollView 在自动布局中尺寸不正确

ios - Swift Heart Rate Variability 结果从 ms 到 double 的转换

swift - HealthKit 授权在 iOS 10 beta 1 中因未处理的 NSException 而崩溃

ios - 在 Parse Cloud 或 healthkit 中存储 StepCount 的推荐方法是什么?

ios - 当我从纵向模式更改为横向模式时,按钮背景图像消失

ios - 自定义 UILongPressGestureRecognizer 的问题不适用于 VoiceOver

ios - 删除 View 和搜索栏之间的边框

healthkit - 通过 REST API 查询 HealthKit 数据

ios - 查询 HealthKit 的相关性,但使用 anchor