---
title: "init(sampleType:predicate:updateHandler:)"
framework: healthkit
role: symbol
role_heading: Initializer
path: "healthkit/hkobserverquery/init(sampletype:predicate:updatehandler:)"
---

# init(sampleType:predicate:updateHandler:)

Instantiates and returns a query that monitors the HealthKit store and responds to changes.

## Declaration

```swift
init(sampleType: HKSampleType, predicate: NSPredicate?, updateHandler: @escaping @Sendable (HKObserverQuery, @escaping HKObserverQueryCompletionHandler, (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 doc://com.apple.healthkit/documentation/HealthKit/HKSampleType class (the doc://com.apple.healthkit/documentation/HealthKit/HKQuantityType, doc://com.apple.healthkit/documentation/HealthKit/HKCategoryType, doc://com.apple.healthkit/documentation/HealthKit/HKWorkoutType, and doc://com.apple.healthkit/documentation/HealthKit/HKCorrelationType classes)
- `predicate`: A predicate that limits the samples matched by the query. Pass nil if you want to receive updates for every new sample of the specified type.
- `updateHandler`: A block that is called when a matching sample is saved to or deleted from the HealthKit store. This block takes the following parameters:

## Mentioned in

Executing Observer Queries

## Return Value

Return Value A newly initialized observer query object.

## Discussion

Discussion After instantiating the query, call the HKHealthStore class’s 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 HKHealthStore class’s stop(_:) method. 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 HKAnchoredObjectQuery

## See Also

### Creating Observer Queries

- [Executing Observer Queries](healthkit/executing-observer-queries.md)
- [init(queryDescriptors:updateHandler:)](healthkit/hkobserverquery/init(querydescriptors:updatehandler:).md)
- [HKObserverQueryCompletionHandler](healthkit/hkobserverquerycompletionhandler.md)
