Search code examples
objective-ciosbackgroundmbprogresshud

MBProgressHUD while logging out


Im using the MBProgressHUD to make an overview loading screen while logging out in my ipad app. That progress takes some time because I have to encrypt some bigger files.

Because Im doing it in a background thread and the MBProgressHUD is animating on the main thread, I had to do something to know when my background thread is finished.

As a test, I did it like that:

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDAnimationFade;
hud.labelText = @"Do something...";

[self performSelectorInBackground:@selector(doSomethingElse) withObject:nil];

And method doSomethingElse:

-(void)doSomethingElse
{
    [self encrypt];
    [self performSelectorOnMainThread:@selector(doSomethingElseDone) withObject:nil waitUntilDone:YES];
}

And method doSomethingElseDone:

-(void)logoutInBackgroundDone
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];    
}

The solution works but I think there must be a better way? How can I do that on a better way?

Any help is really appreciated.


Solution

  • You can directly dismiss the MBProgressHUD from doSomethingElse method using dispatch_async

    -(void)doSomethingElse
    {
        [self encrypt];
        dispatch_async(dispatch_get_main_queue(), ^{
             [MBProgressHUD hideHUDForView:self.view animated:YES];
        });
    }