Mitesh jadav
Mitesh jadav

Reputation: 930

HealthKit Read data Heart Rate and BloodPressure 'latest update after sync'

I am syncing Heart Rate and BloodPressure data from HealthKit.

The problem with this approach is when the user enters historic data which will not be synced. How do I perform the same query but with CreationDate (instead of StartDate), or some kind of database ID which will identify the historic value as being newer?

I just want to filter out all the newly created values from healthkit.

-(void)getSpecificHealthKitDataHeartReat:(NSDate*)myDate
{
NSDateFormatter *dtFormat = [[NSDateFormatter alloc] init];

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDate *now = [NSDate date];

NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];

NSDate *startDate = [calendar dateFromComponents:components];

NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0];

NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionNone];

//[HKQuery predicateForObjectWithUUID:(nonnull NSUUID *)]

//Read HeartRate
HKHealthStore *healthStore = [[HKHealthStore alloc] init];
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierStartDate ascending:YES];
HKQuantityType *heartRateType2 = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
HKSampleQuery *sampleQuery2 = [[HKSampleQuery alloc] initWithSampleType:heartRateType2 predicate:predicate limit:0 sortDescriptors:@[timeSortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error)
                               {
                                   if (!results)
                                   {
                                       NSLog(@"There are no heart rate results. The error was: %@.", error);
                                       return;
                                   }
                                   else
                                   {
                                       NSMutableArray *hrArray = [[NSMutableArray alloc]init];
                                       for(HKQuantitySample *samples in results)
                                       {
                                           HKQuantity *hrQuantity = [samples quantity];
                                           // double hr = [hrQuantity doubleValueForUnit:[HKUnit unitFromString:@"count/min"]];
                                           double hr = [hrQuantity doubleValueForUnit: [[HKUnit countUnit] unitDividedByUnit:[HKUnit minuteUnit]]];
                                           
                                           NSLog(@"hr %f",hr);
                                           NSLog(@"startDate  %@",samples.startDate);
                                           NSLog(@"endDate  %@",samples.endDate);
                                           
                                       }
                                   }
                               }];
// Execute the query
[healthStore executeQuery:sampleQuery2];
   }

Upvotes: 1

Views: 360

Answers (1)

Allan
Allan

Reputation: 7363

Use HKAnchoredObjectQuery (documentation here). It is designed for exactly this use case.

Upvotes: 0

Related Questions