如何强制HKQuery加载最新的步骤计数? [英] How to force a HKQuery to load the most recent steps counts?
问题描述
目前我正在尝试使用 HKStatisticsQuery
来获取特定时间间隔之间的步数。我自己正在通过摇动
手机进行测试。但是,似乎我得到的结果不是最近的结果,除非:
Currently I'm trying to use HKStatisticsQuery
to get the steps count between a certain time interval. I'm doing test by shaking
the phone myself. However, it seems that the result I get is not the most recent one, unless:
- 我打开
健康.app
,让它在后台运行,然后在我的应用中再次进行测试; - 我打开
UP应用
,让它在后台运行,然后在我的应用程序中再次进行测试。
- I open the
Health.app
, keep it running in background, and do the test again in my app; - I open the
UP app
, keep it running in background, and do the test again in my app.
如果我强制退出 Health.app
或 UP app
,我的应用将无法再次获取最新数据。因此UP必须做一些我缺少的事情,但我找不到 HKHealthStore
中的任何重载方法,或者 HKQuery / HKStatisticsQuery
。
And if I force-quit the Health.app
orUP app
, my app will not be able to get the most recent data again. So UP must be doing something I'm missing, but I can't find there's any "reload" like method in HKHealthStore
, or any related options in HKQuery/HKStatisticsQuery
.
我正在使用的代码非常简单,如下所示。我想知道是否有任何权限或我遗漏的任何内容。
The code I'm using is quite straight forward as below. I wonder if there's any permissions or anything I'm missing.
let predicate = HKQuery.predicateForSamplesWithStartDate(date_start, endDate: NSDate(), options: HKQueryOptions.StrictStartDate)
var type = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)
var query = HKStatisticsQuery(quantityType: type,
quantitySamplePredicate: predicate,
options: .CumulativeSum | .SeparateBySource,
completionHandler: { query, stats, error in ( /*logs here*/ ) })
let healthStore = HKHealthStore()
healthStore.executeQuery(query)
编辑:我还尝试将一些数据写入 HealthKit
但是查询没有得到更新。
I also tried to write some data to HealthKit
but the query doesn't get updated.
Edit2:当我说最近的步骤计数时,我的意思是:1。执行HKQuery;摇动手机; 3.再次执行HKQuery。运行上面的代码2次,我总是得到相同的结果,但如果我在后台离开Health.app或 UP
应用程序,最新的查询得到了更新的结果。
when I said "most recent steps counts" I meant something like: 1. execute HKQuery; 2. shake phone; 3. execute HKQuery again. Running the code above for 2 times and I always get the same results, but if I left Health.app or the UP
app in the background, the latest query got the updated results.
我还尝试调用其他一些API,如:
I also tried to call some other APIs like:
healthStore.enableBackgroundDeliveryForType(type, frequency:.Immediate, withCompletion:{
(success:Bool, error:NSError!) -> Void in
let authorized = healthStore.authorizationStatusForType(type)
LF.log("HEALTH callback success", success)
LF.log("HEALTH callback authorized", type)
})
if HKHealthStore.isHealthDataAvailable() == false {
LF.log("HEALTH data not available")
return
} else {
LF.log("HEALTH OK")
}
几乎没有理由但是试图暗中触发某种背景刷新。但这些尝试都没有奏效。
For almost no reason but try to secretly "trigger" some sort of background refresh. But none of these attempts worked.
推荐答案
HealthKit并不总是有最新的用户步数和距离计数旅行。它会定期导入这些值,这些值实际上来自CoreMotion.framework,并响应某些事件。如果正在运行的应用程序具有打开的 HKObserverQuery
或 HKStatisticsCollectionQuery
,那么HealthKit会将值传输到客户端,否则样本将是只是上次导入的快照。
HealthKit does not always have an up-to-date count of the user's steps and distance travelled. It imports these values, which actually come from CoreMotion.framework, periodically and in response to certain events. If a running application has an open HKObserverQuery
or HKStatisticsCollectionQuery
then HealthKit will stream the values to the client but otherwise the samples are just a snapshot from the last import.
因此,如果您想观察样本类型的更改,您应该使用 HKObserverQuery
订阅更新然后再次查询HealthKit以获取最新值。但更有效的方法是使用 HKStatisticsCollectionQuery
,它有一个更新处理程序,将作为匹配谓词的样本的统计信息进行调用。
So if you'd like to observe changes for a sample type, you should subscribe to updates using an HKObserverQuery
and then query HealthKit again for the latest values. A more efficient approach would be to use HKStatisticsCollectionQuery
, though, which has an update handler that will be invoked as the statistics for the samples matching the predicates change.
最后,如果您只对最近7天的最新步数或最近行程感兴趣,那么我建议您考虑直接使用CoreMotion.framework。
Finally, if you're only interested up-to-date step counts or distance travelled for at most the past 7 days then I recommend that you consider using CoreMotion.framework directly instead.
这篇关于如何强制HKQuery加载最新的步骤计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!