MBProgressHUD showAnimated: whileExecutingBlock: not working

1.4k views Asked by At

I'm using a method for scanning Bluetooth device, which I import from another framework. The scanning method would take a while, and it'll block the GUI, which is what we never want to happen.

I'm also having MBProgressHud, trying to show a hud while scanning, but it's not working (hud not showing up). Any help?

Here's the code I'm currently using:

    [hud showAnimated:YES whileExecutingBlock:^{
          self.btDevices = [Util scanBT];
    }];

Edit 1: Okay, so if I use this code, it'll still block my UI for a while, then all suddenly continue to run.

    hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.labelText = @"Now scanning";

    hud.dimBackground = YES;
    hud.opacity = 0.5;
    [hud show:YES];
    [hud hide:YES afterDelay:5.0];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
        self.btDevices = [Util scanBT];
    });

Edit 2: Ok, I will post all my block of code:

    hud = [[MBProgressHUD alloc] initWithView:[self getTopView:self.view]];
    hud.labelText = @"Now scanning";

    hud.dimBackground = YES;
    hud.opacity = 0.5;

    [hud showAnimated:YES whileExecutingBlock:^{
          self.btDevices = [Util scanBT];
     }];

    dispatch_queue_t myqueue = dispatch_queue_create("queue", NULL);
    dispatch_async(myqueue, ^{

        //Whatever is happening in the BT scanning method will now happen in the background
        dispatch_async(dispatch_get_main_queue(), ^{

            [MBProgressHUD hideHUDForView:[self getTopView:self.view] animated:YES];
        });
    });

/** Recursion to get the top most view in view layer. */
- (UIView *)getTopView:(UIView *)view
{
    if ([view.superview class]) {
        return [self getTopView:view.superview];
    }

    return view;
}

I'm request scanning bt in a popover, but I want to show HUD in a main View, so I write a block to retrieve the main view. Maybe that's where the problem occur?

1

There are 1 answers

8
Omer Waqas Khan On

Try this:

In your viewDidload or the method where you want to place it

MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [hud setDimBackground:YES];
    [hud setOpacity:0.5f];
    [hud show:YES];
    [hud hide:YES afterDelay:5.0];
 [self performSelector:@selector(startScanning) withObject:nil afterDelay:5.0];

And your method

- (void) startScanning {

    self.btDevices = [Util scanBT];
}

OR I think you should try it running

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
    // Insert task code here
self.btDevices = [Util scanBT];
});

Try it with completion block

[hud showAnimated:YES whileExecutingBlock:^{
       self.btDevices = [Util scanBT];
    } completionBlock:^{
      //code for after completion 
    }];

OR you can also try this

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

dispatch_queue_t myqueue = dispatch_queue_create("queue", NULL);
dispatch_async(myqueue, ^{

    //Whatever is happening in the BT scanning method will now happen in the background
   self.btDevices = [Util scanBT];

    dispatch_async(dispatch_get_main_queue(), ^{

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