如何在应用程序处于后台时查询 HealthKit (HKWorkout) 更新?
How to query for HealthKit (HKWorkout) updates when app is in background?
编辑以添加我基于 WWDC 2016 的“充分利用 Healthkit”谈话的更新代码,但除非我打开应用程序,否则我仍然无法启动新锻炼的打印声明?
我正在尝试在 iPhone 上观察保存在 Apple Watch 上的新锻炼。下面是我 运行 在 didFinishLaunching
中的代码。为了测试它,我 运行 Xcode 在我的 iPhone App...building 和 运行 上,然后导航回主屏幕。然后在我的手表上开始并保存锻炼,但是我的打印语句没有打印在控制台上。我错过了什么?
func startObservingNewWorkouts() {
let sampleType = HKObjectType.workoutType()
//1. Enable background delivery for workouts
self.healthStore.enableBackgroundDelivery(for: sampleType, frequency: .immediate) { (success, error) in
if let unwrappedError = error {
print("could not enable background delivery: \(unwrappedError)")
}
if success {
print("background delivery enabled")
}
}
//2. open observer query
let query = HKObserverQuery(sampleType: sampleType, predicate: nil) { (query, completionHandler, error) in
self.updateWorkouts() {
completionHandler()
}
}
healthStore.execute(query)
}
func updateWorkouts(completionHandler: @escaping () -> Void) {
var anchor: HKQueryAnchor?
let sampleType = HKObjectType.workoutType()
let anchoredQuery = HKAnchoredObjectQuery(type: sampleType, predicate: nil, anchor: anchor, limit: HKObjectQueryNoLimit) { [unowned self] query, newSamples, deletedSamples, newAnchor, error in
self.handleNewWorkouts(new: newSamples!, deleted: deletedSamples!)
anchor = newAnchor
completionHandler()
}
healthStore.execute(anchoredQuery)
}
func handleNewWorkouts(new: [HKSample], deleted: [HKDeletedObject]) {
print("new sample added = \(new.last.startTime!)")
}
事实证明这段代码有效,只是我在模拟器中进行了测试,显然观察者查询在模拟器中 运行 时会触发 NOT 但它会在 [=13= 时触发] 在设备上
编辑以添加我基于 WWDC 2016 的“充分利用 Healthkit”谈话的更新代码,但除非我打开应用程序,否则我仍然无法启动新锻炼的打印声明?
我正在尝试在 iPhone 上观察保存在 Apple Watch 上的新锻炼。下面是我 运行 在 didFinishLaunching
中的代码。为了测试它,我 运行 Xcode 在我的 iPhone App...building 和 运行 上,然后导航回主屏幕。然后在我的手表上开始并保存锻炼,但是我的打印语句没有打印在控制台上。我错过了什么?
func startObservingNewWorkouts() {
let sampleType = HKObjectType.workoutType()
//1. Enable background delivery for workouts
self.healthStore.enableBackgroundDelivery(for: sampleType, frequency: .immediate) { (success, error) in
if let unwrappedError = error {
print("could not enable background delivery: \(unwrappedError)")
}
if success {
print("background delivery enabled")
}
}
//2. open observer query
let query = HKObserverQuery(sampleType: sampleType, predicate: nil) { (query, completionHandler, error) in
self.updateWorkouts() {
completionHandler()
}
}
healthStore.execute(query)
}
func updateWorkouts(completionHandler: @escaping () -> Void) {
var anchor: HKQueryAnchor?
let sampleType = HKObjectType.workoutType()
let anchoredQuery = HKAnchoredObjectQuery(type: sampleType, predicate: nil, anchor: anchor, limit: HKObjectQueryNoLimit) { [unowned self] query, newSamples, deletedSamples, newAnchor, error in
self.handleNewWorkouts(new: newSamples!, deleted: deletedSamples!)
anchor = newAnchor
completionHandler()
}
healthStore.execute(anchoredQuery)
}
func handleNewWorkouts(new: [HKSample], deleted: [HKDeletedObject]) {
print("new sample added = \(new.last.startTime!)")
}
事实证明这段代码有效,只是我在模拟器中进行了测试,显然观察者查询在模拟器中 运行 时会触发 NOT 但它会在 [=13= 时触发] 在设备上