Search code examples
iphoneiosobjective-cuiviewcontrollerviewdidload

Start computations automatically only after view has appeared


I have a view controller that just shows progress during calculations. I put the method calls

in viewDidLoad but the problem is the view only appears once the calculations are done! How

could I automatically launch the calculations after the view has appeared on screen?


Solution

  • You may use GCD. Here is Raywenderlich tutorial

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
            //Calculations
            dispatch_async(dispatch_get_main_queue(), ^{
                //Update UI must be here
            });
        });
    }