RCTAppleHealthKit+Queries.m 3.18 KB
Newer Older
1 2 3 4 5 6 7 8 9
//
//  RCTAppleHealthKit+Queries.m
//  RCTAppleHealthKit
//
//  Created by Greg Wilson on 2016-06-26.
//  Copyright © 2016 Greg Wilson. All rights reserved.
//

#import "RCTAppleHealthKit+Queries.h"
10
#import "RCTAppleHealthKit+Utils.h"
11 12 13 14

@implementation RCTAppleHealthKit (Queries)


15
- (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType predicate:(NSPredicate *)predicate completion:(void (^)(HKQuantity *, NSDate *, NSDate *, NSError *))completion { NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO];
16

17
    HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType
18 19 20 21 22
                                                  predicate:predicate
                                                  limit:1
                                                  sortDescriptors:@[timeSortDescriptor]
                                                  resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
                                                      if (!results) {
23
                                                          if (completion) {
24
                                                              completion(nil, nil, nil, error);
25
                                                          }
26 27 28 29 30 31 32
                                                          return;
                                                      }

                                                      if (completion) {
                                                          // If quantity isn't in the database, return nil in the completion block.
                                                          HKQuantitySample *quantitySample = results.firstObject;
                                                          HKQuantity *quantity = quantitySample.quantity;
33 34 35
                                                          NSDate *startDate = quantitySample.startDate;
                                                          NSDate *endDate = quantitySample.endDate;
                                                          completion(quantity, startDate, endDate, error);
36 37
                                                      }
                                                  }];
38

39 40 41 42 43 44
    [self.healthStore executeQuery:query];
}



- (void)fetchSumOfSamplesTodayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit completion:(void (^)(double, NSError *))completionHandler {
45
    NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesToday];
46
    
47 48 49 50
    HKStatisticsQuery *query = [[HKStatisticsQuery alloc] initWithQuantityType:quantityType
                                                          quantitySamplePredicate:predicate
                                                          options:HKStatisticsOptionCumulativeSum
                                                          completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) {
51 52 53 54 55 56 57 58 59 60 61 62
        HKQuantity *sum = [result sumQuantity];
        
        if (completionHandler) {
            double value = [sum doubleValueForUnit:unit];
            completionHandler(value, error);
        }
    }];
    
    [self.healthStore executeQuery:query];
}

@end