Instantiates and returns a query that monitors the HealthKit store and responds to changes.
- iOS 8.0+
- Mac Catalyst 13.0+
- watchOS 2.0+
A predicate that limits the samples matched by the query. Pass
nilif you want to receive updates for every new sample of the specified type.
A block that is called when a matching sample is saved to or deleted from the HealthKit store. This block takes the following parameters:
A reference to the query calling this block.
If you have registered for background updates, you must call this completion handler as soon as you are done processing the incoming data. This tells HealthKit that you have successfully received the background update. Additionally, you should only call the completion handler when you are using background updates. For more information on using this completion handler, see
Query Completion Handler
If an error occurs, this parameter contains an object describing the error; otherwise, it is
A newly initialized observer query object.
After instantiating the query, call the
execute(_:) method to run this query. Observer queries are long-running tasks. They continue to run on an anonymous background thread and call their results handler whenever they detects relevant changes to the HealthKit store. To stop a query, call the
The provided update handler block is called every time samples matching this query are saved to or deleted from the HealthKit store. You often need to launch other queries from inside this block to get the updated data. In particular, you can use Anchored Object Queries to retrieve the list of new samples that have been added to the store. For more information, see