diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index f18ceb36ed7c7cd30c61908cd3f33e0524c99ee7..4a484fea2829b60b9ad60ec88f55e98027fc8072 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -86,6 +86,8 @@ NSDictionary *elem = @{ @"value" : @(value), + @"sourceName" : [[[sample sourceRevision] source] name], + @"sourceId" : [[[sample sourceRevision] source] bundleIdentifier], @"startDate" : startDateString, @"endDate" : endDateString, }; @@ -115,7 +117,7 @@ completion:(void (^)(NSArray *, NSError *))completion { NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:asc]; - + // declare the block void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error); // create and assign the block @@ -126,25 +128,25 @@ } return; } - + if (completion) { NSMutableArray *data = [NSMutableArray arrayWithCapacity:1]; - + dispatch_async(dispatch_get_main_queue(), ^{ if (type == [HKObjectType workoutType]) { for (HKWorkout *sample in results) { double energy = [[sample totalEnergyBurned] doubleValueForUnit:[HKUnit kilocalorieUnit]]; double distance = [[sample totalDistance] doubleValueForUnit:[HKUnit mileUnit]]; NSString *type = [RCTAppleHealthKit stringForHKWorkoutActivityType:[sample workoutActivityType]]; - + NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]; NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate]; - + bool isTracked = true; if ([[sample metadata][HKMetadataKeyWasUserEntered] intValue] == 1) { isTracked = false; } - + NSString* device = @""; if (@available(iOS 11.0, *)) { device = [[sample sourceRevision] productType]; @@ -154,7 +156,7 @@ device = @"iPhone"; } } - + NSDictionary *elem = @{ @"activityId" : [NSNumber numberWithInt:[sample workoutActivityType]], @"activityName" : type, @@ -167,27 +169,27 @@ @"start" : startDateString, @"end" : endDateString }; - + [data addObject:elem]; } } else { for (HKQuantitySample *sample in results) { HKQuantity *quantity = sample.quantity; double value = [quantity doubleValueForUnit:unit]; - + NSString * valueType = @"quantity"; if (unit == [HKUnit mileUnit]) { valueType = @"distance"; } - + NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]; NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate]; - + bool isTracked = true; if ([[sample metadata][HKMetadataKeyWasUserEntered] intValue] == 1) { isTracked = false; } - + NSString* device = @""; if (@available(iOS 11.0, *)) { device = [[sample sourceRevision] productType]; @@ -197,7 +199,7 @@ device = @"iPhone"; } } - + NSDictionary *elem = @{ valueType : @(value), @"tracked" : @(isTracked), @@ -207,22 +209,22 @@ @"start" : startDateString, @"end" : endDateString }; - + [data addObject:elem]; } } - + completion(data, error); }); } }; - + HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:type predicate:predicate limit:lim sortDescriptors:@[timeSortDescriptor] resultsHandler:handlerBlock]; - + [self.healthStore executeQuery:query]; } @@ -234,16 +236,16 @@ return; } [self.bridge.eventDispatcher sendAppEventWithName:@"observer" body:@""]; - + // Theoretically, HealthKit expect that copletionHandler would be called at the end of query process, // but it's unclear how to do in in event paradigm - + // dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 5); // dispatch_after(delay, dispatch_get_main_queue(), ^(void){ // completionHandler(); // }); }]; - + [self.healthStore executeQuery:query]; [self.healthStore enableBackgroundDeliveryForType:type frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError * _Nullable error) { NSLog(@"success %s print some error %@", success ? "true" : "false", [error localizedDescription]);