xcode HealthKit读取数据心率和血压'同步后的最新更新'

6tdlim6h  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(132)

我正在同步HealthKit中的心率和血压数据。
这种方法的问题是当用户输入的历史数据不会被同步时,我如何执行相同的查询,但使用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];
   }
nsc4cvqm

nsc4cvqm1#

使用HKAnchoredObjectQuery(文档here)。它正是为此用例而设计的。

相关问题