Commit 76fa1a2d authored by Greg Wilson's avatar Greg Wilson

added sleep category query to queries

parent ea7b0437
...@@ -38,4 +38,10 @@ ...@@ -38,4 +38,10 @@
limit:(NSUInteger)lim limit:(NSUInteger)lim
completion:(void (^)(NSArray *, NSError *))completionHandler; completion:(void (^)(NSArray *, NSError *))completionHandler;
- (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate
limit:(NSUInteger)lim
completion:(void (^)(NSArray *, NSError *))completion;
@end @end
...@@ -106,6 +106,102 @@ ...@@ -106,6 +106,102 @@
} }
- (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate
limit:(NSUInteger)lim
completion:(void (^)(NSArray *, NSError *))completion {
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate
ascending:false];
// declare the block
void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error);
// create and assign the block
handlerBlock = ^(HKSampleQuery *query, NSArray *results, NSError *error) {
if (!results) {
if (completion) {
completion(nil, error);
}
return;
}
if (completion) {
NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
dispatch_async(dispatch_get_main_queue(), ^{
for (HKCategorySample *sample in results) {
HKCategoryType *catType = sample.categoryType;
NSInteger *val = sample.value;
// HKQuantity *quantity = sample.quantity;
// double value = [quantity doubleValueForUnit:unit];
NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
NSDictionary *elem = @{
@"value" : @(( (float) *val )),
@"catType" : catType.identifier,
@"startDate" : startDateString,
@"endDate" : endDateString,
};
[data addObject:elem];
}
completion(data, error);
});
}
};
// HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType
// predicate:predicate
// limit:lim
// sortDescriptors:@[timeSortDescriptor]
// resultsHandler:handlerBlock];
HKCategoryType *categoryType =
[HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
// HKCategorySample *categorySample =
// [HKCategorySample categorySampleWithType:categoryType
// value:value
// startDate:startDate
// endDate:endDate];
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:categoryType
predicate:predicate
limit:lim
sortDescriptors:@[timeSortDescriptor]
resultsHandler:handlerBlock];
[self.healthStore executeQuery:query];
}
- (void)fetchCorrelationSamplesOfType:(HKQuantityType *)quantityType - (void)fetchCorrelationSamplesOfType:(HKQuantityType *)quantityType
unit:(HKUnit *)unit unit:(HKUnit *)unit
predicate:(NSPredicate *)predicate predicate:(NSPredicate *)predicate
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment