0

I have a method to get contacts from the address book and doing some stuff with them ("getContactInformation").

This process is a bit long (a few seconds) and after this process I show a new ViewController. To make it friendly to the user I would like to use MBProgressHUD to show an activity indicator at the beginning of the process and hide it at the end.

Which is the best way to do it? I've test this:

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Retrieving information...";

[self getContactInformation];

[MBProgressHUD hideHUDForView:self.view animated:YES];

[self presentViewController:newController animated:NO completion:nil];

But it doesn't work (It doesn't show the indicator). Anyone can help me?

Thank you in advance

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
rai212
  • 711
  • 1
  • 6
  • 20

2 Answers2

1

Keep a separate method for [self presentViewController:newController animated:NO completion:nil];. And try calling that method after some particular delay. That should be solving the problem.

Vivek T
  • 398
  • 6
  • 22
1
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    [self getContactInformation];
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.mapView animated:YES];
    });
});

This will spawn a thread to run getContactInformation and won't block the main thread which is required to do UI changes (such as displaying the HUD).

I'm guessing the main thread is getting locked up when you get contact info and doesn't have time to update to show the HUD. Once the method is complete, it gets the main thread again to update the UI (removing the HUD).

Padin215
  • 7,444
  • 13
  • 65
  • 103