2

Most of my data is coming from a web service and it can take quite a bit of time. Especially on lesser networks.

The screen comes in black. I can see from NSLogs that the data is sowing down the pipe but the indicator is not showing. Then when it is sone the indicator pops up ever so briefly then disappears.

  - (void)viewDidLoad
    {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[HUD showUIBlockingIndicatorWithText:@"Fetching DATA"];
[self callDatabase];
[HUD hideUIBlockingIndicator];
    }
Adnan
  • 5,025
  • 5
  • 25
  • 44
Raymond
  • 477
  • 2
  • 5
  • 15

1 Answers1

2

It sounds like your [self callDatabase] method may be blocking the main thread. When the main thread is blocked modifications to the view hierarchy will not be reflected on screen. Can you put the database work in the background? Perhaps like this:

[HUD showUIBlockingIndicatorWithText:@"Fetching DATA"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self callDatabase];
    dispatch_async(dispatch_get_main_queue(), ^{
        [HUD hideUIBlockingIndicator];
    });
});

This would cause the callDatabase method to run in the background, not blocking your main thread. Then when the database work has completed the HUD would be hidden on the main thread. Note that it's important to modify the view hierarchy (and generally, "do UI work") only on the main thread.

Aaron Golden
  • 7,092
  • 1
  • 25
  • 31
  • This actually causes the data to take longer to load and the HUD still doesn't show – Raymond Mar 01 '14 at 01:45
  • It works when I set it up on an IBAction but not on the viewdidload – Raymond Mar 01 '14 at 01:47
  • @Raymond Try putting this in `viewWillAppear:` instead of `viewDidLoad`. – rmaddy Mar 01 '14 at 02:03
  • @Raymond the reason is callDatabase may finish so fast and when view did appear the hud has already been hided. – Danyun Liu Mar 01 '14 at 03:30
  • @Raymond make sure initial `showUIBlockingIndicatorWithText:` is being called on the main thread so it can alter the UI. You can wrap everything in another `dispatch_async(dispatch_get_main_queue(), ^{ ... });` if this code is initially called from another thread. – ansonl Dec 16 '19 at 00:15