Today I needed to debug some code that is using HealthKit, running observer queries in the background, also when the app is not running.
The app could create multiple observer queries on the same sample types, but only the one created last should be considered the correct one.
There is no much information in the documentation. For instance:
- Does the execution of a new observer query stop other queries on the same data type started earlier?
- If you start an observer query running in the background, then kill your app and launch the app again, is the query still running?
After some empirical tests, these are my conclusions:
- You can execute multiple observer queries on the same sample types in your app. They will all execute independently when new samples are added to HealthKit.
- On the next app launch, they will be stopped and you need to start a new observer query.
I still need to test what happens when your users update the app. Is the query stopped? Does the user need to open the app after update to get a new query to be executed?
Please let me know if you have evidence that disproves my theory above. I would be happy to be enlightened on this subject.