HKAnchoredObjectQuery在后台无法可靠返回 [英] HKAnchoredObjectQuery not returning reliably in background

查看:164
本文介绍了HKAnchoredObjectQuery在后台无法可靠返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当观察到新数据输入时,我都试图使我的应用程序将HealthKit数据与我们的数据库同步. 尽管在逻辑上有些歧义我相信我已经设法启用了样本类型的后台交付,并让观察者在需要时做出反应.

I'm trying to make my app synchronize HealthKit data with our database whenever new data input is observed. Despite some ambiguity in the logic I believe I've managed to enable background delivery for the sample types and have the observers react when needed.

但是,在观察者的updatehandler中,我需要创建一个HKAnchoredObjectQuery来从HealthKit中获取最新结果,但是当我的应用程序在后台运行时,这些查询不能可靠地返回.通常,当我在HealthKit中添加样本点时,锚定查询只是被执行,但是只有当我将应用程序带回前台时,它们才会返回.

However, in the observer's updatehandler I need to create an HKAnchoredObjectQuery for fetching the most recent results from HealthKit, but these queries don't return reliably when my app is running in the background. Often when I add sample points in HealthKit, the anchored queries just get executed, but they return only when I bring the app back in the foreground.

其他时候,他们无需我激活应用程序即可立即返回.一个可能的问题是,我所有的观察者查询都会触发其updatehandler,从而创建多个可能返回也可能不返回的AnchoredObjectQueries.我已经在链接线程中进行了进一步解释,因为它可能无关.

Other times they return immediately without me having to activate the app. One possible issue is that all of my observer queries fire their updatehandlers, thus creating multiple AnchoredObjectQueries that may or may not return. I've explained that further in the linked thread, because it may be unrelated.

这是我从HKObserverQuery的updatehandler调用的函数的示例:

Here's an example of the function I'm calling from HKObserverQuery's updatehandler:

func synchronizeRecentData(sampleType: HKSampleType, observerQuery: HKObserverQuery) {

    let completionHandler: (HKAnchoredObjectQuery, [HKSample]?, Int, NSError?) -> Void = {
        [unowned self] query, results, newAnchor, error in

        if error != nil {
            abort()
        }

        // Update queryAnchor
        self.queryAnchors[sampleType] = newAnchor

        guard let receivedNewSamples = results as? [HKQuantitySample] else {
            abort()
        }

        // Handle received samples here
    }

    let query = HKAnchoredObjectQuery(type: sampleType,
                                      predicate: nil,
                                      anchor: self.queryAnchors[sampleType]!,
                                      limit: HKObjectQueryNoLimit,
                                      completionHandler: completionHandler)
    healthKitStore.executeQuery(query)
}

推荐答案

根据HealthKit文档:

According to the HealthKit docs:

与观察者查询不同,这些更新包括一个项目列表,这些项目 已添加或删除;但是,锚定对象查询不能 注册用于后台交付.有关更多信息,请参见 HKAnchoredObjectQuery .

Unlike the observer query, these updates include a list of items that have been added or removed; however, anchored object queries cannot be registered for background delivery. For more information, see HKAnchoredObjectQuery.

可以注册后台传递的唯一查询是 HKObserverQuery

The only query that can register for background delivery is the HKObserverQuery

这篇关于HKAnchoredObjectQuery在后台无法可靠返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆