Skip to content

Instantly share code, notes, and snippets.

@jault3
Last active August 29, 2015 14:08
Show Gist options
  • Save jault3/263bb908bda7fb36578b to your computer and use it in GitHub Desktop.
Save jault3/263bb908bda7fb36578b to your computer and use it in GitHub Desktop.
SaveNewLog.m
HKUnit *bpmUnit = [[HKUnit countUnit] unitDividedByUnit:[HKUnit minuteUnit]];
NSDate *startDate = (NSDate *)[_data objectForKey:kStartDate];
NSDate *endDate = [NSDate date];
NSMutableDictionary *metadata = [NSMutableDictionary dictionary];
[metadata setValue:[NSNumber numberWithDouble:_txtBeginningHeartRate.text.doubleValue] forKey:kBeginningHeartRate];
[metadata setValue:[NSNumber numberWithDouble:_txtEndingHeartRate.text.doubleValue] forKey:kEndingHeartRate];
// send the heart beat off to Catalyze and HealthKit
[CatalyzeStore saveQuantitySampleWithUnitString:@"mi" value:_txtDistance.text.doubleValue identifier:HKQuantityTypeIdentifierDistanceWalkingRunning startDate:startDate endDate:endDate metadata:metadata completion:^(BOOL success, NSError *error) {
if (success) {
NSMutableDictionary *metadata = [NSMutableDictionary dictionary];
// this is optional to store these booleans, but it may make for easier querying later
[metadata setValue:[NSNumber numberWithBool:YES] forKey:kBeginningHeartRate];
[CatalyzeStore saveQuantitySampleWithUnitString:bpmUnit.unitString value:_txtBeginningHeartRate.text.doubleValue identifier:HKQuantityTypeIdentifierHeartRate startDate:startDate endDate:startDate metadata:metadata completion:^(BOOL success, NSError *error) {
if (success) {
NSMutableDictionary *metadata = [NSMutableDictionary dictionary];
[metadata setValue:[NSNumber numberWithBool:YES] forKey:kEndingHeartRate];
[CatalyzeStore saveQuantitySampleWithUnitString:bpmUnit.unitString value:_txtEndingHeartRate.text.doubleValue identifier:HKQuantityTypeIdentifierHeartRate startDate:endDate endDate:endDate metadata:metadata completion:^(BOOL success, NSError *error) {
if (success) {
// all 3 completed, we're done
_saved = YES;
[self.navigationController popViewControllerAnimated:YES];
} else {
NSLog(@"error saving end heart rate");
}
}];
} else {
NSLog(@"error saving beginning heart rate");
}
}];
} else {
NSLog(@"error saving distance");
}
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment