我正在尝试学习 GCD,所以我还没有完全掌握它的工作原理。出于某种原因,我在调用以下方法后遇到帧率永久下降的情况。如果我不使用调度函数而只是在主循环中写入数据,帧速率将保持在 60。我不知道为什么。
-(void)saveDataFile {
_hud = [MBProgressHUD showHUDAddedTo:self.parentView animated:YES];
_hud.labelText = NSLocalizedString(@"Saving data...", nil);
dispatch_queue_t myQueue = dispatch_queue_create("myQueueName", NULL);
dispatch_async(myQueue, ^(void) {
@autoreleasepool {
id data = [self.model getData];
if (data != nil) {
NSString *filePath = @"myPath";
[data writeToFile:filePath atomically:YES];
}
}
dispatch_async(dispatch_get_main_queue(), ^(void) {
[_hud hide:YES];
});
});
}
最佳答案
已解决。我从这个问题开始关注 HUD 的实现:MBProgressHUD not showing
基本上,我需要移除 HUD 而不是简单地隐藏它。否则 HUD 动画继续,我看不到,因此导致帧率下降。
-(void)saveDataFile {
// create HUD and add to view
MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:self.parentView];
hud.labelText = NSLocalizedString(@"Saving data...", nil);
hud.delegate = self;
[self.parentView addSubview:hud];
// define block for saving data
void (^saveData)() = ^() {
@autoreleasepool {
id data = [self.model getData];
if (data != nil) {
NSString *filePath = @"myPath";
[data writeToFile:filePath atomically:YES];
}
}
}
// use HUD convenience method to run block on a background thread
[hud showAnimated:YES whileExecutingBlock:saveData];
}
// remove hud when done!
//
- (void)hudWasHidden:(MBProgressHUD *)hud {
[hud removeFromSuperview];
}
关于ios - dispatch_async 和帧率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14178397/