jeongmin.cha
jeongmin.cha

Reputation: 878

health kit observer query always called when the app becomes active

The resultHandler of HKObserverQuery is always called when the app becomes active (background -> foreground)

But, I wrote the code of the query in didFinishLaunchingWithOptions method in AppDelegate.swift. I know the method is called when the app is launched not the app become active.

func application(application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    healthStore.authorizeHealthKit {
        ...
    }
}
// other AppDelegate methods are empty

How do I make the handler of the query called only when my app is launched?

Upvotes: 3

Views: 1462

Answers (1)

Allan
Allan

Reputation: 7363

Why do you want to prevent the updateHandler from firing?

You can't control when the updateHandler of an HKObserverQuery fires while the query is running. You can prevent it from being called at all by stopping the query. It is designed to be called whenever there might be new HealthKit data matching your predicate. You should design your updateHandler such that it doesn't matter when it is called.

If you really wanted the observer query to not fire when your app returns to the foreground, you would need to stop the query completely with -[HKHealthStore stopQuery:] when your app enters the background, before it suspends.

Upvotes: 3

Related Questions