3

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:

http://www.sudzc.com/

..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
}
gotnull
  • 26,454
  • 22
  • 137
  • 203

3 Answers3

6

If you're using MBProgressHUD in a situation where a callback is performed, then there is another approach. Initialize the HUD before beginning your background process with

MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

instead of

HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];

and then put the following in your callback method:

[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];

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.

Bot
  • 11,868
  • 11
  • 75
  • 131
Matt
  • 1,563
  • 12
  • 13
1

Don't do the network stuff on the main thread because this will block the UI. In your case the MBProgressHUD will not be animating or not even show.

So don't use performSelectorOnMainThread. Instead call the method connectToService directly. The MBProgressHUD will detach a new thread by itself when you call showWhileExecuting.

Felix
  • 35,354
  • 13
  • 96
  • 143
  • You're talking about doing the following: [HUD showWhileExecuting:@selector(connectToService) onTarget:self withObject:nil animated:YES] which doesn't work the way I expect it to work, because the HUD appears for only a brief moment and then disappears before the results are displayed and the callback method is never fired. – gotnull Feb 06 '11 at 23:44
0

Figured this out eventually by modifying my Utility class. I was performing the main selector twice!

gotnull
  • 26,454
  • 22
  • 137
  • 203