1

I'm trying to learn GCD, so I don't have a full grasp on how it works yet. For some reason, I experience a permanent drop in frame rate after I call the following method. If I don't use the dispatch functions and simply write the data on the main loop, the frame rate stays at 60. I don't know why.

-(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];

            });

        });


    }
madth3
  • 7,275
  • 12
  • 50
  • 74
anna
  • 2,723
  • 4
  • 28
  • 37
  • I'd assume that creating a queue is an expensive operation, so maybe you should avoid doing that each time you have to save. Instead, either create the queue and keep a reference to it in your instance, or since you apparently just want to run that in the background, dispatch to one of the existing global queues (`dispatch_get_global_queue` with an according priority). \[EDIT\] just read it's a *permanent* drop, I have no idea why you'd get a permanent framerate drop. I still recommend that you don't repeatedly call `dispatch_queue_create` though. – zneak Jan 06 '13 at 04:14
  • After some trouble-shooting, the problem appears to be related to HUD. If I take out those calls, the drop in frame rate doesn't happen. I must be implementing it incorrectly. Will go take a closer look at that class.. – anna Jan 06 '13 at 16:09

1 Answers1

1

Solved. I followed the implementation of HUD from this question: MBProgressHUD not showing

Basically, I need to remove the HUD rather than simply hide it. Otherwise the HUD animation continued, invisible to me, hence causing the drop in frame rate.

-(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];
}
Community
  • 1
  • 1
anna
  • 2,723
  • 4
  • 28
  • 37