I have the following code, which connects to a web service and returns an array of Categories.
I am using the following wrapper for SOAP web services:
..and the following MBProgressHUD for the activity indicator:
https://github.com/jdg/MBProgressHUD
I would like to have a HUD progress indicator indicate that it’s connecting and grabbing results. I’m currently using MBProgressHUD to achieve the desired effect, however I’m noticing that it’s not quite working properly. The progress indicator disappears before the actual cells in my tableView are loaded and displayed. I also use the MBProgressHUD in different areas of my application, but usually every time I connect to the web service to grab results.
Could someone lead me in the right direction as to how to fix the below code to work properly? I think it may relate to the fact that the callback method is fired after the progress indicator is displayed. Not quite sure how to implement MBProgressHUD properly with a callback method.
This is my horrible attempt at “trying” to get it working.
- (void)showHUD {
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
// Add HUD to screen.
[self.navigationController.view addSubview:HUD];
// Register for HUD callbacks so we can remove it from the window at the right time.
HUD.delegate = self;
HUD.labelText = @"Loading";
// Show the HUD while the provided method executes in a new thread
[HUD showWhileExecuting:@selector(runLocalNotificationHandler) onTarget:self withObject:nil animated:YES];
}
- (void)runLocalNotificationHandler {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self performSelectorOnMainThread:@selector(connectToService) withObject:nil waitUntilDone:YES];
[pool release];
}
- (void)connectToService {
Service1 *service = [[Service1 alloc] init];
[service GetCategories:self action:@selector(handlerGetCategories:)];
[service release];
}
- (void)handlerGetCategories:(id)value {
// parse objects returned from array, populate array, reload table view data, etc
}
If you’re using
MBProgressHUDin a situation where a callback is performed, then there is another approach. Initialize the HUD before beginning your background process withinstead of
and then put the following in your callback method:
This way you know exactly when your HUD is shown and closed. At the very least, it will help you debug where the real problem is by seeing what changes after using the new approach.