RCTAppleHealthKit+Queries.m 2.93 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 15

@implementation RCTAppleHealthKit (Queries)


- (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType predicate:(NSPredicate *)predicate completion:(void (^)(HKQuantity *, 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, error);
25
                                                          }
26 27 28 29 30 31 32 33 34 35
                                                          return;
                                                      }

                                                      if (completion) {
                                                          // If quantity isn't in the database, return nil in the completion block.
                                                          HKQuantitySample *quantitySample = results.firstObject;
                                                          HKQuantity *quantity = quantitySample.quantity;
                                                          completion(quantity, error);
                                                      }
                                                  }];
36

37 38 39 40 41 42
    [self.healthStore executeQuery:query];
}



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

@end