Reputation: 317
I have a TabBarController which contains 3 TabBarItems. When the second TabBarItem is clicked a UITableView is built and loaded. It takes a long enough time to load the table, with nothing going, and it would be a courtesy to the user to indicate that the next scene is on the way. However, I have not been able to get this to work. I've tried using GCD to implement the loading mask, but it only displays it once the table is loaded. That's not much good after 20 seconds have passed.
I've tried this code to respond to the TabBarItem click:
[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
^{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"dispatch");
[self.navigationController.view.superview addSubview:HUD];
[MBProgressHUD hideHUDForView:viewController.view animated:YES];
});
});
I've also tried it as the UITableview is being built:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_sync(queue, ^{
UIImage * image = [UIImage imageWithData:imageData];
dispatch_sync(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
[cell.merchItemImageView setImage:image];
NSLog(@"shortDesc==%@",merchitem.itemName);
NSLog(@"itemPrice==%@",merchitem.itemPrice);
cell.merchItemShortDesc.text = [NSString stringWithFormat:@"%@",merchitem.itemName];
cell.merchItemPrice.text = [NSString stringWithFormat:@"$%@",merchitem.itemPrice];
});
});
Upvotes: 0
Views: 261
Reputation: 7661
Try to find in your table building/loading code where it starts to heavily block the user interface and move that code to a secondary method. At the end of the original method call MBProgressHUD
and queue a selector on the current run loop after a zeroth delay. What this does is run first the pending UI operations (including MBProgressHUD's dialogs) and later your blocking code. I do something similar for a blocking CSV file importation, where I first show a SVProgressHUD
alert, then perform the unresponsive task.
So if your code looks like this:
step1;
step2;
really_long_step3;
you could split it like
step1;
[MBProgressHUD updates];
[self performSelector:@selector(do_remaining_steps)
withObject:nil afterDelay:0];
}
- (void)do_remaining_steps
{
step2;
really_long_step3;
[MBProgressHUD reportSomeSuccessScreen];
}
The trick here is to find where in the tab switching and view creation hierarchy does your code block and how you can split it to let UI updates happen before the long running loading. Hopefully this all happens in your -(void)viewDidLoad
method, so viewDidLoad
would be queuing the do_remaining_steps
selector at the end before updating the HUD.
Upvotes: 1