健康工具包观察者查询总是在应用程序激活时调用
health kit observer query always called when the app becomes active
HKObserverQuery
的 resultHandler
总是在应用程序激活时调用(后台 -> 前台)
但是,我在AppDelegate.swift
中的didFinishLaunchingWithOptions
方法中写了查询的代码。我知道该方法是在应用程序启动时调用的,而不是应用程序激活时调用的。
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
healthStore.authorizeHealthKit {
...
}
}
// other AppDelegate methods are empty
如何让查询的处理程序仅在我的应用程序启动时调用?
为什么要阻止 updateHandler 触发?
当查询为 运行 时,您无法控制 HKObserverQuery
的 updateHandler 何时触发。您可以通过停止查询来阻止它被调用。它被设计为在可能有新的 HealthKit 数据与您的谓词匹配时被调用。您应该设计您的 updateHandler
,使其在调用时无关紧要。
如果您真的希望当您的应用程序 returns 进入前台时不触发观察者查询,您需要在您的应用程序进入后台时使用 -[HKHealthStore stopQuery:]
完全停止查询,在此之前暂停。
HKObserverQuery
的 resultHandler
总是在应用程序激活时调用(后台 -> 前台)
但是,我在AppDelegate.swift
中的didFinishLaunchingWithOptions
方法中写了查询的代码。我知道该方法是在应用程序启动时调用的,而不是应用程序激活时调用的。
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
healthStore.authorizeHealthKit {
...
}
}
// other AppDelegate methods are empty
如何让查询的处理程序仅在我的应用程序启动时调用?
为什么要阻止 updateHandler 触发?
当查询为 运行 时,您无法控制 HKObserverQuery
的 updateHandler 何时触发。您可以通过停止查询来阻止它被调用。它被设计为在可能有新的 HealthKit 数据与您的谓词匹配时被调用。您应该设计您的 updateHandler
,使其在调用时无关紧要。
如果您真的希望当您的应用程序 returns 进入前台时不触发观察者查询,您需要在您的应用程序进入后台时使用 -[HKHealthStore stopQuery:]
完全停止查询,在此之前暂停。