我想立即从Apple Watch到iPhone收集心率数据,但是即使遵循Apple's document,我也发现无法创建和更新HKAnchorObjectQuery(在HKObserverQuery中)。
此外,HKObserverQuery在开始执行应用程序时仅工作一次。
我已经尝试了2周,再也没有想法了...
代码如下:
func createHeartRateStreamingQuery(workoutStartDate: NSDate) -> HKQuery?{
let sampleType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!
let query = HKObserverQuery(sampleType: sampleType, predicate: nil) {
query, completionHandler, error in
if error != nil {
// Perform Proper Error Handling Here...
print("*** An error occurred while setting up the observer. ***")
abort()
}else{
print("sampleType initiated: ",sampleType)
}
let anchoredQuery = HKAnchoredObjectQuery(type: sampleType, predicate: nil, anchor: self.anchor, limit: Int(HKObjectQueryNoLimit)) { (query1, sampleObjects, deletedObjects, newAnchor, error) -> Void in
print("anchored query initiated!")
guard let newAnchor = newAnchor
else {return}
self.anchor = newAnchor
self.updateHeartRate(sampleObjects) //use to display the heart rate data
}
anchoredQuery.updateHandler = {(query1, samples, deleteObjects, newAnchor, error) -> Void in
self.anchor = newAnchor!
self.updateHeartRate(samples) //use to display the heart rate data
print("anchored query updating!")
}
completionHandler()
}
return query
}
let query = createHeartRateStreamingQuery()
healthStore.executeQuery(query)
寻找您的回覆!
Here's我的另一个问题与此有关。
最佳答案
您不能在HKObserverQuery中使用HKSampleQuery。 HKObserverQuery的updateHandler。观察者查询的处理程序可以在后台模式下执行,但示例查询之一不能在后台模式下执行。
关于ios - HKObserverQuery无法在iPhone上运行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36528826/