1

我正在同步 HealthKit 中的 Heart Reat、BloodPressure 数据。

这种方法的问题是当用户输入不会同步的历史数据时。如何执行相同的查询,但使用 CreationDate(而不是 StartDate),或某种将历史值标识为较新的数据库 ID?

我只想从 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];
   }
4

1 回答 1

0

使用HKAnchoredObjectQuery此处的文档)。它正是为这个用例而设计的。

于 2018-09-14T01:25:21.130 回答