Skip to content

Instantly share code, notes, and snippets.

@jault3 jault3/SaveNewLog.m
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.