Search code examples
objective-ciosuiviewcontrollerios5.1mbprogresshud

Removing MBProgressHUD when dialog is displayed and selector method not complete


I am using code to call a method and show the HUD as below

    HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];

HUD.delegate = self;
HUD.labelText = @"Signing you up";

// myProgressTask uses the HUD instance to update progress
[HUD showWhileExecuting:@selector(processFieldEntries) onTarget:self withObject:nil animated:YES];

Within the processFieldEntries I have some error checking which then shows a dialog. Like below:

showDialog:
if (textError) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:errorText message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
    [alertView show];
    return;
}

This then causes a crash, probably because they are on the same thread or not removing the HUD from the view.

My question is should I add different code to ensure they run on different threads? And what should I add to the processFieldEntries method to then remove the HUD as it is called showWhileExecuting...


Solution

  •     MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.delegate = self;
        hud.labelText = @"Signing you up";
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.05 * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [self processFieldEntries];
            // Do something...
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            });