所以根据苹果的说法,我需要在updateHandler被添加到HK的数据触发后调用HKObserverQueryCompletionHandler。
但是只要我调用completionHandler,观察者查询就会停止在后台进行任何更新。
下面是我的代码:
guard let sampleType = sample as? HKQuantityType else { return nil }
let query = HKObserverQuery(sampleType: sampleType, predicate: nil, updateHandler: { query, completionHandler, error in
completionHandler()
IamExecutingHKStatisticsCollectionQueryHere()
})
healthStore?.execute(query)
healthStore?.enableBackgroundDelivery(for: sampleType, frequency: .hourly, withCompletion: { success, error in
})如果我不调用completionHandler,一切正常,但是我很长一段时间都没有测试过。
发布于 2017-10-11 10:46:51
调用completionHandler()表明您已经完成了对新数据的处理。只有在处理了为响应updateHandler调用而执行的查询的结果之后,才调用它。如果你像现在这样提早调用completionHandler(),系统将在你有机会处理数据之前停止在后台运行你的应用程序。
https://stackoverflow.com/questions/46669260
复制相似问题