0

所以根据Apple的说法,我需要在数据被添加到HKHKObserverQueryCompletionHandler之后调用。updateHandler

但是一旦我调用了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,一切正常,但我从未测试过很长时间..

4

1 回答 1

0

调用completionHandler()表示您已完成处理新数据。只有在您处理了响应updateHandler被调用而执行的查询的结果后才调用它。如果您completionHandler()像现在这样提前调用,系统将在您有机会处理数据之前停止在后台运行您的应用程序。

于 2017-10-11T02:46:51.310 回答