Class

HKDocumentQuery

A query that returns a snapshot of all matching documents currently saved in the HealthKit store.

Declaration

@interface HKDocumentQuery : HKQuery

Overview

Use an HKDocumentQuery object to search for documents in the HealthKit store. You can provide a predicate to filter the search results, a sort order for the returned samples, or even a limit to the number of samples returned.

Document queries are immutable: The query’s properties are set when the query is first created. They cannot change.

Executing Queries

To create and execute a query, perform the following steps:

  1. Create the document type by calling the HKObjectType class’s documentTypeForIdentifier: method.

  2. (optionally) Create an NSPredicate object to filter the search results.

  3. (optionally) Create an array of NSSortDescriptor objects to provide the sort order for the results.

  4. Instantiate a new query by calling the initWithDocumentType:predicate:limit:sortDescriptors:includeDocumentData:resultsHandler: method.

  5. In the results handler, handle any errors and process the results.

    Note, the query returns the results in batches and may call the results handler more than once. If the done parameter is set to NO, the query is still active and will call the results handler with additional results. If the done parameter is set to YES, the query is complete.

guard let cdaType = HKObjectType.documentType(forIdentifier: .CDA) else {
    fatalError("Unable to create a CDA document type.")
}
 
var allDocuments = [HKDocumentSample]()
let cdaQuery = HKDocumentQuery(documentType: cdaType,
                               predicate: nil,
                               limit: HKObjectQueryNoLimit,
                               sortDescriptors: nil,
                               includeDocumentData: false) {
                                
                                (query, resultsOrNil, done, errorOrNil) in
                                
                                guard let results = resultsOrNil else {
                                    if let queryError = errorOrNil {
                                        // Handle the query error here...
                                    }
                                    
                                    return
                                }
                                
                                allDocuments += results
                                
                                if done {
                                    // the allDocuments array now contains all the samples returned by the query.
                                    // Handle the documents here...
                                }
}

Subclassing Document Queries

Like many HealthKit classes, the HKDocumentQuery class should not be subclassed.

Topics

Creating Document Queries

HKObjectQueryNoLimit

A value indicating that the query returns all the matching samples in the HealthKit store.

Accessing the Document Query’s Properties

includeDocumentData

A Boolean value that indicates whether the sample includes the full document’s data.

limit

The maximum number of documents the receiver will return upon completion.

sortDescriptors

An array of sort descriptors that specify the order of the results returned by this query.

Relationships

Inherits From

See Also

Common Query Types

HKSampleQuery

A general-purpose query that returns a snapshot of all the matching samples currently saved in the HealthKit store.

HKQuantitySeriesSampleQuery

A query that accesses the series data associated with a quantity sample.

HKAnchoredObjectQuery

A query that returns only recent changes to the HealthKit store, including a snapshot of new changes and continuous monitoring as a long-running query.

HKObserverQuery

A long-running query that monitors the HealthKit store and updates your app when the HealthKit store saves or deletes a matching sample.

HKCorrelationQuery

A query that performs complex searches based on the correlation’s contents, and returns a snapshot of all matching samples.

HKHeartbeatSeriesQuery

A query that returns the heartbeat data contained in a heartbeat series sample.

HKQuery

An abstract class for all the query classes in HealthKit.