views:

1013

answers:

2

I have a UITableView with the following code:

- (void)viewDidLoad {
    [super viewDidLoad];
    parser = [[XMLParser alloc] init];
    [parser parseXML];

My problem is that the launch takes too long because it's parsing everything before displaying the view controller with the UITableView. Also, if I set up another UITableView and parse another XML (in a different tab) I tap to go the other tab, but then it hangs while it parses the other XML, and once it's done, then it display the UITableView.

I have looked for information on when to start the parsing, reload the UITableView and how to show a loading screen while the parsing code runs, but have not been able to come up with anything.

Anyone have any ideas?

+1  A: 

If by loading screen you mean an activity indicator then trying to add the indicator animated before parsing could potentially not work because when you parse on the main thread it blocks and does not let the indicator appear on screen. To get around this i would do the parsing on a background thread, this should allow your indicator to appear, when the parsing is done, ahve the parsing object send a message to your viewController so i t knows its ready to show the tableview. (i should mention that UIKit is not thread safe and you should not try to update any UI elements from the background thread without using performSelectorInMainThread)

Daniel
I already have an activity indicator in the status bar, but the app still appear to 'hang' for a while, while the parsing takes place. I want to show a view while loading, which says "Loading" or something and then fades away, or perhaps just show a blank UITableView until the parsing is done. Once the parsing is done the UITableView refreshes..
Canada Dev
So make a view that does that and add the subview before the parsing...
Daniel
+3  A: 

You can call something like

[parser performSelectorInBackground:@selector(parseXML) withObject:nil];

on your main thread to run the parseXML code in a different thread. Just be careful to not update the ui from that thread. To update the UI from the parser thread, you'll need to call something like

[self performSelectorOnMainThread:@selector(XMLUpdated:) withObject:self waitUntilDone:NO];
bwinton