0

I'm actually using the NSXMLParser class for parse an XML file hosted on a server. While the parser is doing his job I want to show a progress HUD grabbed there : https://github.com/jdg/MBProgressHUD

Here is how I show the Progress HUD :

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:NO];
hud.labelText = @"Update"; 

Then I start the parser using a class I have made :

[dm parseXMLAtURL:[NSURL URLWithString:@"myXML.xml.php"]];

Here is the full class method :

- (void)parseXMLAtURL:(NSURL *)XMLUrl {
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:XMLUrl];
    [parser setDelegate:self];
    NSLog(@"start");
    [parser parse];    
}

I obviously call the MBProgressHUD before I parse the XML but there is no way : the MBProgressHUD is shown when the parser finish his job.

I think this problem could be due to the stack management, when he do the longest job before others (parsing the full XML takes like 6-7 secondes) Am I right or it could be another stuff? And if I'm right how can I manage this?

Community
  • 1
  • 1
Edelweiss
  • 621
  • 1
  • 9
  • 24

2 Answers2

3

You are wright that it has to do with timing. Since all is done on the same thread. Just delay the parsing of XML by a couple of milliseconds:

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:NO];
hud.labelText = @"Update"; 
[dm performSelector:@selector(parseXMLAtURL:)  withObject:[NSURL URLWithString:@"myXML.xml.php"] afterDelay:0.1f];

Not as nice as running the XML parsing on a new thread but it will work.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Works good, I'll keep it if I can't do it with Klaus's solution (using different thread) Thank you for you quick answer – Edelweiss Aug 10 '12 at 12:34
  • This is a general proposition on iOS processing and UI updates do not go together, if you are in an event loop and the two things must occur more or less at once, performSelector..after delay to complete the event loop before starting a UI update (or vice versa) is always a good idea. corretc answer for what you needed. In this case either way the UI is blocked so fuller multithreading wont buy you a whole lot. – deleted_user Aug 10 '12 at 13:28
2

I can't say for sure from the code you have posted, but it could very well be because the XML parser is blocking your main thread.

To fix it, spawn of your XML parsing to anaother thread using dispatch_async or similar methods. This is also explained in the HUD Readme under Usage.

Regards

KlausCPH
  • 1,816
  • 11
  • 14