<!--
{
  "documentType" : "article",
  "framework" : "HealthKit",
  "identifier" : "/documentation/HealthKit/executing-activity-summary-queries",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Executing Activity Summary Queries"
}
-->

# Executing Activity Summary Queries

Create and run activity summary queries.

## Discussion

Use activity summary queries to read [`HKActivitySummary`](/documentation/HealthKit/HKActivitySummary) objects from the HealthKit store. Apps can display summary information using an activity ring view ([`HKActivityRingView`](/documentation/HealthKitUI/HKActivityRingView) on iOS or <doc://com.apple.documentation/documentation/WatchKit/WKInterfaceActivityRing> on watchOS).

### Create the Predicate

Start by creating a predicate for summaries over the previous week.

```swift
let calendar = NSCalendar.current
let endDate = Date()
 
guard let startDate = calendar.date(byAdding: .day, value: -7, to: endDate) else {
    fatalError("*** Unable to create the start date ***")
}

let units: Set<Calendar.Component> = [.day, .month, .year, .era]

var startDateComponents = calendar.dateComponents(units, from: startDate)
startDateComponents.calendar = calendar

var endDateComponents = calendar.dateComponents(units, from: endDate)
endDateComponents.calendar = calendar

// Create the predicate for the query
let summariesWithinRange = HKQuery.predicate(forActivitySummariesBetweenStart: startDateComponents,
                                             end: endDateComponents)
```

### Create the Query

Then create an activity summary query by calling the [`init(predicate:resultsHandler:)`](/documentation/HealthKit/HKActivitySummaryQuery/init(predicate:resultsHandler:)) initializer.

```swift
let query = HKActivitySummaryQuery(predicate: summariesWithinRange) { (query, summariesOrNil, errorOrNil) -> Void in
    
    guard let summaries = summariesOrNil else {
        // Handle any errors here.
        return
    }
    
    for summary in summaries {
        // Process each summary here.
    }
    
    // The results come back on an anonymous background queue.
    // Dispatch to the main queue before modifying the UI.
    
    DispatchQueue.main.async {
        // Update the UI here.
    }
}
```

The query returns an array of activity summary objects. The results handler should check for errors before processing the summaries. It should also dispatch updates to the user interface back to the main thread.

### Run the Query

After instantiating the query, call the HealthKit store’s [`execute(_:)`](/documentation/HealthKit/HKHealthStore/execute(_:)) method.

```swift
store.execute(query)
```

This method runs the query on an anonymous background queue. When the query is complete, it executes the results handler on the same background queue (but not necessarily on the same thread).

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)