问题描述
在iOS 9.3中,HKStatisticsCollectionQuery似乎非常慢.一年前每小时一次返回有效卡路里的统计信息可能需要40秒钟以上的时间.
It seems HKStatisticsCollectionQuery is incredibly slow in iOS 9.3. It can take upwards of 40 seconds to return hourly statistics for a year for active calories where it took 1 or less before.
let predicate = HKQuery.predicateForSamplesWithStartDate(anchorDate, endDate: endDate, options: [])
let query = HKStatisticsCollectionQuery(quantityType: quantityType,
quantitySamplePredicate: predicate,
options: statisticOptions,
anchorDate: anchorDate,
intervalComponents: interval)
推荐答案
经过数小时的反复试验,我发现HKStatisticsCollectionQuery不是线程友好的.为了解决问题,我使用了以下异步NSOperation: https://gist.github.com/calebd/93fa347397cec5f88233
After many hours of trial and error I have found that HKStatisticsCollectionQuery is not thread friendly. In order to solve the problem I used this async NSOperation:https://gist.github.com/calebd/93fa347397cec5f88233
当然还有一个NSOperationQueue,以强制HKStatisticsCollectionQuerys同步执行.一旦完成此操作,每个查询将花费不到半秒钟的时间.
And of course an NSOperationQueue in order to force the HKStatisticsCollectionQuerys to be performed synchronously. Once I did that each query took less than half a second.
这篇关于如何在iOS 9.3中加快HKStatisticsCollectionQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!