ios - dispatch_async 和帧率

标签 ios grand-central-dispatch mbprogresshud

我正在尝试学习 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/

相关文章:

IOS - 粘性 UICollectionView header (如 Apple App Store)

ios - Swift 如何知道 NSValue 中装箱的对象类型?

ios - Xcode 中的 Storyboard 引用,我们应该在哪里使用它?

iphone - NSURLConnection 与委托(delegate) vs initWithContentsOfURL : with Grand Central Dispatch

iphone - 如何使用两个后续的 MBProgressHUD 消息

ios - 使用 iOS 4.0 库时的向后兼容性

ios - 使用 GCD 计时器间隔播放声音不符合预期

ios - MBprogressHUD 未按预期显示

ios - HUD 在 iOS 中不工作

objective-c - 为什么我们不能在当前队列上使用 dispatch_sync?