1

我正在尝试学习 GCD,所以我还没有完全掌握它是如何工作的。出于某种原因,在调用以下方法后,我的帧速率会永久下降。如果我不使用dispatch函数,只是在主循环上写数据,帧率一直停留在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];

            });

        });


    }
4

1 回答 1

1

解决了。我从这个问题关注了HUD的实现:MBProgressHUD not show

基本上,我需要移除 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];
}
于 2013-01-06T16:56:52.973 回答