The problem is not that the network call doesn't take place if done in viewDidLoad
(because that is the right place to do it), but rather that the attempt to add the SVProgressHUD
in viewDidLoad
won't work, because this is too early in the process of constructing the view
for SVProgressHUD
to figure out where to put the HUD.
One simple solution would be to have viewDidLoad
defer the invocation of this, to give the UI a chance to get to a state that SVProgressHUD
can correctly place itself in the view hierarchy. As silly as it seems, you can just dispatch the code back to the main loop, which gives the UI a chance to catch up:
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];
[[MyServer sharedManager] fetchFromServerwithCompletionHandler:^(NSArray *elements, BOOL error) {
_elements = elements;
[_elementsTableView reloadData];
[SVProgressHUD dismiss];
}];
});
}