Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lag between viewWillAppear and viewDidAppear

I have a strange issue using presentViewController as part of a library.

The code that is using the library calls this method. It takes up to 12 seconds from calling presentViewController to running the completion block. But not all the time normally it's almost instantaneous.

However if I touch any where on the screen while it is "lagging" it will fire instantly.

-(void) advancedMenuWithPresentingViewController
                    :(UIViewController *)presentingViewController
            animated:(BOOL)animated
             onClose:(void (^)(void))onClose
             onPrint:(void (^)(NSString *, NSString *))onPrint{
    AdvancedMenuViewController *amc = [[AdvancedMenuViewController alloc] init];
        AdvancedMenuViewController * __weak weakAmc = amc;

        [amc setOnClose:^(void)
         {
             [weakAmc dismissViewControllerAnimated:animated completion:^{
                 onClose();
             }];
         }];
        [amc setOnPrint:onPrint];
    //Time from here
    [presentingViewController presentViewController:amc
                                               animated:animated
                                             completion:^{
              //To here
         }];
}

viewDidLoad and viewWillAppear are called without any lag and then there is a long delay (unless you touch the screen) until viewDidAppear is called.

There is nothing inside any of these methods that would slow it down. As it normally works fine.

If any one could shed any light on this issue I would be most grateful, the most confusing part is that if you interact with the screen after firing advancedMenuWithPresentingViewController it will occur instantly.

like image 871
Ben Avery Avatar asked Nov 20 '13 04:11

Ben Avery


2 Answers

Replacing

[presentingViewController presentViewController:amc
                                               animated:animated
                                             completion:^{
              //To here
         }];

with

    dispatch_async(dispatch_get_main_queue(), ^{
        [presentingViewController presentViewController:amc
                                               animated:animated
                                             completion:^{
        //To here
        }];
    });

Resolved the issue.

Thanks to rmaddys suggestion.

like image 90
Ben Avery Avatar answered Sep 30 '22 11:09

Ben Avery


Run the app in Instruments with the Time Profiler. That should at least tell you if you have some slow drawing code or if something in UIKit is slowing it down.

like image 22
Ashton-W Avatar answered Sep 30 '22 11:09

Ashton-W