2

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?

Eddie
  • 1,903
  • 2
  • 21
  • 46

1 Answers1

1

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];
    });
});
Omer Waqas Khan
  • 2,423
  • 4
  • 33
  • 62
  • Excuse me, can you show me where the MBProgressHud is showing? – Eddie Jun 09 '15 at 10:59
  • Can you please add more code to your question, so that I could try to catch the issue – Omer Waqas Khan Jun 09 '15 at 11:16
  • I just updated it. Well, I'm not using much code here. Just wonder why MBProgressHUD isn't showing. I can't use another approach since the scanBT method doesn't have anything like completion handler or delegate. – Eddie Jun 09 '15 at 11:24
  • Edited my answer check it now – Omer Waqas Khan Jun 09 '15 at 11:40
  • The background task is cool, I get it. It doesn't block my GUI anymore. But the hud still not showing. I don't get it. – Eddie Jun 09 '15 at 11:56
  • 1
    What version of MBProgressHUD are you using. 0.9.1 is buggy. https://github.com/jdg/MBProgressHUD/issues/279 – Fergal Rooney Jun 09 '15 at 12:04
  • I'm using 0.5 // MBProgressHUD.m Version 0.5 Created by Matej Bukovinski on 2.4.09. /=====/ Since it's come to thread, I'd prefer a thread where Hud is animating, the task is block the main thread. After it finish, release the main thread and release hud. – Eddie Jun 09 '15 at 12:06
  • 1
    XCode has a neat feature where you can capture the view hierarchy at any point in time when running in simulator. When you think the progress view is showing, go to XCode -> Debug -> View Debugging -> Capture View Hierarchy. This will help you debug where it is in the view hierarchy. – Fergal Rooney Jun 09 '15 at 12:21