Show loading screen while parsing and then update UITableView?

I have a UITableView with the following code:

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

      

My problem is that it takes too long to start up because it parses everything before displaying the view controller with the UITableView. Also, if I set up another UITableView and parse a different XML (in a different tab), I touch the other tab, but then it freezes when it parses another XML and after that it displays the UITableView.

I've searched for information on when to start parsing, reload the UITableView, and how to show the loading screen while the parsing code is running, but couldn't come up with anything.

Does anyone have any idea?

+2


source to share


2 answers


You can call something like

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

      



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

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

      

+3


source


If when you load the screen you mean an activity indicator, then trying to add an indicator animated before parsing could potentially fail, because when parsing the main thread, it blocks and prevents the indicator from appearing on the 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, if the parse object sends a message to your view controller, so I know its ready to show the table view. (I should mention that UIKit is not thread safe and you shouldn't try to update any UI elements from a background thread without using performSelectorInMainThread)



+1


source







All Articles