所以,我试图让我的应用程序读取HealthKit
数据。我有一个从主应用程序视图控制器调用的函数,它会在另一个类中查询该月的所有健康数据。然后在数据数组从计算类中的单独函数返回到视图控制器中的单独函数之前进行一些计算。
由于数据量大,每个查询大约需要 2 秒。我希望能够异步设置它们,当它们全部返回时,我可以更新 UI。
问题是,我每个月都调用该函数,该函数开始运行HKSampleQueries
,但它们没有按顺序返回,并且它们返回所需的时间各不相同。这意味着我最终会在一组数据计算中途更改变量,因为下一组刚刚开始。
我只知道解决这个问题的两种方法:
在调用每个计算之前设置延迟,如下所示:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{}
但这会浪费应用时间
或者我可以多次复制代码并为每个月调用不同的类。但这似乎是愚蠢和低效的。
所以问题是。我如何有效地共享每次使用不同变量运行多次的代码。干杯
函数示例:
在视图控制器中:
HeartRateCalculator *commonClassTwo =[[HeartRateCalculator alloc] init];
[commonClassTwo calculateData:0];
[commonClassTwo calculateData:-1];
[commonClassTwo calculateData:-2];
在心率计算器中
-(void)calculateData:(NSInteger)monthsBack{
//Some other stuff
//Generate monthPeriodPredicate based on monthsBack integer
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:heartRate predicate:monthPeriodPredicate limit:200000 sortDescriptors:@[timeSortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
//Finish Calculations, call other functions (ie. [self doThis];) and then return
//When calculations return, monthPeriodPredicate is always value of the last predicate to be called, not the one that the HKSampleQuery was made with.
}
[healthStoreFive executeQuery:query];
完整代码:
-(void)calculateData:(NSInteger)monthsBack withCompletionBlock:(void(^)())completionBlock {//0 Means only current month, 2 means this month and last month and month before
//for(NSInteger i=0; i>=monthsBack; i--){
//monthForCalculation = monthsBack;
NSDateComponents *components = [[NSCalendar currentCalendar] components: NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:[NSDate date]];
NSDateComponents *adjustableComponent = [[NSDateComponents alloc] init];
[adjustableComponent setMonth:monthsBack];
[adjustableComponent setDay:-[components day]+1];
[adjustableComponent setHour:-[components hour]];
[adjustableComponent setMinute:-[components minute]];
[adjustableComponent setSecond:-[components second]];
startOfMonth = [[NSCalendar currentCalendar] dateByAddingComponents:adjustableComponent toDate:[NSDate date] options:0];
adjustableComponent = [[NSDateComponents alloc] init];
[adjustableComponent setMonth:1];
NSDate *endOfMonth = [[NSCalendar currentCalendar] dateByAddingComponents:adjustableComponent toDate:startOfMonth options:0];
NSDate *secondEarlier = [endOfMonth dateByAddingTimeInterval:-1];
components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay fromDate:secondEarlier];
daysInMonth = [components day];
NSPredicate *monthPeriodPredicate = [HKQuery predicateForSamplesWithStartDate:startOfMonth endDate:endOfMonth options:HKQueryOptionStrictStartDate];
healthStoreFive = [[HKHealthStore alloc] init];
HKQuantityType *heartRate = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO];
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:heartRate predicate:monthPeriodPredicate limit:200000 sortDescriptors:@[timeSortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
NSMutableArray *dataValues = [[NSMutableArray alloc] init];
NSMutableArray *dataDates = [[NSMutableArray alloc] init];
for (HKQuantitySample *sample in results) {
[dataValues addObject:[NSNumber numberWithFloat:[sample.quantity doubleValueForUnit:[[HKUnit countUnit] unitDividedByUnit:[HKUnit minuteUnit]]]]];
[dataDates addObject:sample.startDate];
}
monthForCalculation = monthsBack;
chronologicalDataValues = [[NSMutableArray alloc] init];
chronologicalDataDates = [[NSMutableArray alloc] init];
chronologicalDataValues = [[[dataValues reverseObjectEnumerator] allObjects] mutableCopy];
chronologicalDataDates = [[[dataDates reverseObjectEnumerator] allObjects] mutableCopy];
//dispatch_async(dispatch_get_main_queue(), ^{
if(dataDates.count == 0){
ViewController *commonClass =[[ViewController alloc] init];
[commonClass receiveCalculationData:[[NSMutableArray alloc] init] array:[[NSMutableArray alloc] init] daysToDisplay:[[NSMutableArray alloc] init] chosenMonth:monthForCalculation];
}
else{
NSLog(@"%@", [dataDates objectAtIndex:dataDates.count-1]);
NSLog(@"%@", [dataDates objectAtIndex:0]);
[self calculateDayStringsFromData];
}
completionBlock();
//});
}];
NSLog(@"HKSampleQuery Requested For Heart Rate Data");
[healthStoreFive executeQuery:query];
// } }