Search code examples
iosxcode5mbprogresshud

MBProgessHUD show on method execution


I am writing app that will Sync with address book and keep itself updated to addressbook.

When i am calling the syncing method or when its first launch and the app is copying all addressbook contact to the app data file, i want to show MBProgressHUD to the user.

I don't understand this well, i want the MBProgressHUD to show up on the call of this method, and to hide when its done.

- (void)syncingAddressbookWithCoreDataFile

Can you guide me how to use it in the way that i want?

I am confused with all the dispatch and the queues stuff because i am quite novice in all these things.

UPDATED

How can i make the progress of the indicator fill in as the task gets executed, i know there is a progress property in HUD (i am using MBProgressHUDModeDeterminate), but when i increase its value while my method is executing it doesn't change.. do i need to call something like redisplay?


Solution

  • With this code, you have the simpliest way to do what you want. It's not blocking the UI.

    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView: self.view];
    [self.view addSubview: hud];
    hud.labelText = @"Please wait...";
    
    [hud showAnimated:YES whileExecutingBlock:^{
        [self syncingAddressbookWithCoreDataFile];
    } completionBlock:^{
        // Put here code like reload table, refresh UI (...)
    }];
    

    But warning, as whileExecutingBlock using thread you have to make all UI change on main thread.