故事板界面未立即更新
Storyboard Interface Not Updating Straight Away
所以,虽然我终于掌握了 iOS 开发的窍门,但我遇到了这个....
所以我正在尝试创建一个健康应用程序,它使用健康工具包导入一些用户数据,然后以各种方式处理它等等。
在执行初始导入后,我 运行 通过每个数据点来分析其上的数据。在这段大约 7 秒的时间里,我有一个 UI 进度条,它应该随着数据处理的进行而更新和完成。处理完成后,我还有一些应该更新的 UI 标签。
我添加的 UI 元素的 None 更新,直到处理完成后看起来很长(> 5 秒)和随机(多达 60 秒)。
我从 NSLog 语句中知道处理正确完成并且应该更新进度条和标签的变量是正确的。
phone 确实在该处理时间内经历了高 CPU 负载,所以我想知道这是否可能有关联。
viewdidload方法中获取到心率数据后,调用readHeartRateData方法,分别进行如下处理:
-(void)readHeartRateData {
NSLog(@"Read Heart Rate Data");
HKQuantityType *heartRate = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO];
// construct the query & since we are not filtering the data the predicate is set to nil
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:heartRate predicate:nil limit:1000000 sortDescriptors:@[timeSortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
NSInteger dataPoints = results.count;
self.data1.text = [NSString stringWithFormat:@"Total Data Points Collected: %lu", dataPoints];//Does not update straight away
for (int i=0; i<dataPoints; i++) { // The 'processing' bit
float value1 = (unsigned long)dataPoints;
float progress = (i/value1);
[self.progressView setProgress:progress animated:YES];// No update until after process finished
HKQuantitySample *quantitySample = [results objectAtIndex:i];
NSDate *date = quantitySample.startDate;
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];
timeFormatter.dateFormat = @"HH";
NSString *dateString = [timeFormatter stringFromDate: date];
int check = [dateString intValue];
if(check > 0 && check < 8){
nightCount = nightCount + 1;
HKUnit *beatsPerMinute = [[HKUnit countUnit] unitDividedByUnit:[HKUnit minuteUnit]];
HKQuantity *data = quantitySample.quantity;
double dataValue = [data doubleValueForUnit:beatsPerMinute];
nightTotal = nightTotal + dataValue;
}
}
NSLog(@"Number of Night Data Points: %li", (long)nightCount);
nightAverageRate = nightTotal/nightCount;
NSLog(@"Average Night Heart Rate: %li",(long)nightAverageRate);
self.data4.text = [NSString stringWithFormat:@"Total Night-time Data Points: %lu", (long)nightCount];
self.data5.text = [NSString stringWithFormat:@"Average Night-time BPM: %li", (long)nightAverageRate];
// if there is a data point, dispatch to the main queue
if (results) {
dispatch_async(dispatch_get_main_queue(), ^{
// NSDate *startDate, *endDate;
});
}
}];
// do not forget to execute the query after its constructed
[healthStore executeQuery:query];
}
关于为什么数据处理完成后在更新 UI 标签和进度视图之前存在这种随机延迟的任何建议? CPU 开始时使用率很高,但处理完成后会立即下降。
非常感谢阅读
HKSampleQuery 在后台线程上执行,其 resultsHandler 也在后台线程上执行。
This method runs the query on an anonymous background queue. When the query is complete, it executes the results handler on the background queue. Typically, you dispatch these results back to the main queue to update your user interface
您正沿着 dispatch_async 块的正确路线前进,但您需要将对 UI 的所有更改放入该块
随机延迟的原因是 UI 不会重绘,直到其他东西触发 UI 更新。当你点击屏幕时,我会把钱花在重绘上。
所以,虽然我终于掌握了 iOS 开发的窍门,但我遇到了这个....
所以我正在尝试创建一个健康应用程序,它使用健康工具包导入一些用户数据,然后以各种方式处理它等等。
在执行初始导入后,我 运行 通过每个数据点来分析其上的数据。在这段大约 7 秒的时间里,我有一个 UI 进度条,它应该随着数据处理的进行而更新和完成。处理完成后,我还有一些应该更新的 UI 标签。
我添加的 UI 元素的None 更新,直到处理完成后看起来很长(> 5 秒)和随机(多达 60 秒)。
我从 NSLog 语句中知道处理正确完成并且应该更新进度条和标签的变量是正确的。
phone 确实在该处理时间内经历了高 CPU 负载,所以我想知道这是否可能有关联。
viewdidload方法中获取到心率数据后,调用readHeartRateData方法,分别进行如下处理:
-(void)readHeartRateData {
NSLog(@"Read Heart Rate Data");
HKQuantityType *heartRate = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO];
// construct the query & since we are not filtering the data the predicate is set to nil
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:heartRate predicate:nil limit:1000000 sortDescriptors:@[timeSortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
NSInteger dataPoints = results.count;
self.data1.text = [NSString stringWithFormat:@"Total Data Points Collected: %lu", dataPoints];//Does not update straight away
for (int i=0; i<dataPoints; i++) { // The 'processing' bit
float value1 = (unsigned long)dataPoints;
float progress = (i/value1);
[self.progressView setProgress:progress animated:YES];// No update until after process finished
HKQuantitySample *quantitySample = [results objectAtIndex:i];
NSDate *date = quantitySample.startDate;
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];
timeFormatter.dateFormat = @"HH";
NSString *dateString = [timeFormatter stringFromDate: date];
int check = [dateString intValue];
if(check > 0 && check < 8){
nightCount = nightCount + 1;
HKUnit *beatsPerMinute = [[HKUnit countUnit] unitDividedByUnit:[HKUnit minuteUnit]];
HKQuantity *data = quantitySample.quantity;
double dataValue = [data doubleValueForUnit:beatsPerMinute];
nightTotal = nightTotal + dataValue;
}
}
NSLog(@"Number of Night Data Points: %li", (long)nightCount);
nightAverageRate = nightTotal/nightCount;
NSLog(@"Average Night Heart Rate: %li",(long)nightAverageRate);
self.data4.text = [NSString stringWithFormat:@"Total Night-time Data Points: %lu", (long)nightCount];
self.data5.text = [NSString stringWithFormat:@"Average Night-time BPM: %li", (long)nightAverageRate];
// if there is a data point, dispatch to the main queue
if (results) {
dispatch_async(dispatch_get_main_queue(), ^{
// NSDate *startDate, *endDate;
});
}
}];
// do not forget to execute the query after its constructed
[healthStore executeQuery:query];
}
关于为什么数据处理完成后在更新 UI 标签和进度视图之前存在这种随机延迟的任何建议? CPU 开始时使用率很高,但处理完成后会立即下降。 非常感谢阅读
HKSampleQuery 在后台线程上执行,其 resultsHandler 也在后台线程上执行。
This method runs the query on an anonymous background queue. When the query is complete, it executes the results handler on the background queue. Typically, you dispatch these results back to the main queue to update your user interface
您正沿着 dispatch_async 块的正确路线前进,但您需要将对 UI 的所有更改放入该块
随机延迟的原因是 UI 不会重绘,直到其他东西触发 UI 更新。当你点击屏幕时,我会把钱花在重绘上。