init(sampleType:samplePredicate:completionHandler:)
Instantiates and returns a source query.
Declaration
init(sampleType: HKSampleType, samplePredicate objectPredicate: NSPredicate?, completionHandler: @escaping @Sendable (HKSourceQuery, Set<HKSource>?, (any Error)?) -> Void)Parameters
- sampleType:
The type of sample to search for. This query supports all sample types. Specifically, you can pass any concrete subclass of the Hksampletype class (the Hkquantitytype, Hkcategorytype, Hkworkouttype, and Hkcorrelationtype classes).
- objectPredicate:
A predicate that limits the samples matched by the query. Pass
nilif you want to receive the sources for all the samples of the specified type. - completionHandler:
A block that is called when the query finishes executing. This block takes the following parameters:
- query
A reference to the query calling this block.
- results
A set containing the sources for all the samples that match both the sample type and the object predicate, or
nilif an error occurred.- error
If an error occurs, this parameter contains an object describing the error; otherwise, it is
nil.
Mentioned in
Return Value
A newly initialized sample query object.
Discussion
After instantiating the query, call the HKHealthStore class’s execute(_:) method to run this query. Queries run on an anonymous background queue. As soon as the query is complete, the results handler is executed on the same background queue (but not necessarily on the same thread).