首页 > 解决方案 > HKAnchoredObejctQuery 不返回新结果+不一致

问题描述

这是我用来构建和执行查询的代码,它会在加载视图时以及每当按下视图上的按钮时触发。

private func startHeartRateQuery(quantityTypeIdentifier: HKQuantityTypeIdentifier)
    {
        var myAnchor = lastAnchor
        let query = HKAnchoredObjectQuery(type: HKObjectType.quantityType(forIdentifier: quantityTypeIdentifier)!,
                                          predicate: pred, anchor: myAnchor,
                                          limit: HKObjectQueryNoLimit)
        { (query, samplesOrNil, deletedObjectsOrNil, newAnchor, errorOrNil)
            in guard let samples = samplesOrNil as? [HKQuantitySample], let deletedObjects = deletedObjectsOrNil else
            {
                //error
                print("something bad happened")
                return
            }
            myAnchor = newAnchor!
            
            self.process(samples, type: quantityTypeIdentifier)
            DispatchQueue.main.async {
                updateStatus()
            }
        }
        
        query.updateHandler = { (query, samplesOrNil, deletedObjectsOrNil, newAnchor, errorOrNil) in
            guard let samples = samplesOrNil as? [HKQuantitySample], let deletedObjects = deletedObjectsOrNil else {
                // Properly handle the error.
                print("something bad happened in the update handler")
                return
            }
            
            myAnchor = newAnchor!
            
            print("doing something here in update handler")
            self.process(samples, type: quantityTypeIdentifier)
            DispatchQueue.main.async {
                updateStatus()
            }
            
        }


        // 5
        healthStore.execute(query)
    }

但是,当按下按钮时,我没有收到来自设备的更新心率,如下所示:

126 2021-10-26 22:37:46 +0000
processing...
126 2021-10-26 22:37:46 +0000
processing...
126 2021-10-26 22:37:46 +0000
processing...
126 2021-10-26 22:37:46 +0000
processing...
126 2021-10-26 22:37:46 +0000
processing...

尽管我的手机在这里显示了其他心率条目我的个人心率数据

任何有关获得心率查询一致更新的帮助表示赞赏(目标是获取自上次在应用程序中更新以来的所有最新心率)。

编辑:在相当长的一段时间过去后,我还看到了一些奇怪的行为,它延伸到过去并从那里提取数据以及一些新数据。我根本不理解这个查询的行为,因为它现在不一致。

doing something here in update handler
75 2021-10-26 22:32:07 +0000
processing...
doing something here in update handler
75 2021-10-26 22:32:07 +0000
processing...
doing something here in update handler
75 2021-10-26 22:32:07 +0000
85 2021-10-26 22:38:39 +0000
processing...
doing something here in update handler
75 2021-10-26 22:32:07 +0000
processing...
doing something here in update handler
75 2021-10-26 22:32:07 +0000
85 2021-10-26 22:38:39 +0000
83 2021-10-26 22:38:44 +0000
83 2021-10-26 22:38:49 +0000
84 2021-10-26 22:38:50 +0000
processing...
doing something here in update handler
75 2021-10-26 22:32:07 +0000
85 2021-10-26 22:38:39 +0000
processing...
doing something here in update handler
75 2021-10-26 22:32:07 +0000
processing...
doing something here in update handler
75 2021-10-26 22:32:07 +0000
processing...
doing something here in update handler
75 2021-10-26 22:32:07 +0000

标签: iosswifthealthkit

解决方案


推荐阅读