Class

HKActivitySummaryQuery

A query for read activity summary objects from the HealthKit store.

Declaration

@interface HKActivitySummaryQuery : HKQuery

Overview

Activity summary query objects are mostly immutable. You can assign the query’s updateHandler property after instantiating the object. All other properties must be set when you instantiate the object, and they cannot change.

Executing Activity Summary Queries

The sample code, shown below, builds and executes a query that returns the activity summaries for the previous week.

// Create the date components for the predicate
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSDate *endDate = [NSDate date];
NSDate *startDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:-7 toDate:endDate options:0];
NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitEra;
 
NSDateComponents *startDateComponents = [calendar components:unit fromDate:startDate];
startDateComponents.calendar = calendar;
 
NSDateComponents *endDateComponents = [calendar components:unit fromDate:endDate];
endDateComponents.calendar = calendar;
 
// Create the predicate for the query
NSPredicate *summariesWithinRange =
[HKQuery predicateForActivitySummariesBetweenStartDateComponents:startDateComponents endDateComponents:endDateComponents];
 
// Build the query
HKActivitySummaryQuery *query = [[HKActivitySummaryQuery alloc] initWithPredicate:summariesWithinRange resultsHandler:^(HKActivitySummaryQuery * _Nonnull query, NSArray<HKActivitySummary *> * _Nullable activitySummaries, NSError * _Nullable error) {
 
    if (activitySummaries == nil) {
 
        // Handle the error here...
 
        return;
    }
 
    // Do something with the summaries here...
 
}];
 
// Run the query
[self.store executeQuery:query];

This code performs the following steps:

  1. Creates the date component objects representing the start and end days, as perceived by the user.

  2. Creates a predicate object using the date components.

  3. Creates the query object using the predicate.

  4. Executes the query.

Topics

Initializing Activity Summary Queries

- initWithPredicate:resultsHandler:

Initializes a new active summary query.

Getting Property Data

updateHandler

The handler for monitoring updates to activity summaries saved in the HealthKit store.

Relationships

Inherits From

See Also

Activity Rings

HKActivitySummary

An object that contains the move, exercise, and stand data for a given day.

HKActivityRingView

A view that uses the Move, Exercise, and Stand activity rings to display data from a HealthKit activity summary object.