views:

143

answers:

2

I have a UIView that I want to load when the user clicks a button. There happens to be some data processing that happens as well after I call addSubview that involves parsing an XML file retrieved from the web. The problem is the view doesn't show up until after the data processing even if addSuview is called first. I think I'm missing something here, can anyone help?

Code: I have a "Loading..." view I'm adding as a custom modal (meaning I'm not using the modalViewController). This action is linked to a button in the navigationController.

- (IBAction)parseXml:(id)sender {
      LoadingModalViewController *loadingModal = [[LoadingModalViewController alloc] initWithNibName:@"LoadingModalViewController" bundle:nil];
      [navigationController.view addSubview:loadingModal.view];
      [xmlParser parse];
}
A: 

If you are doing your processing on the main thread, it will block the main thread until its done, which means your UI will become unresponsive and not update until the main thread resumes.

You need to perform your XML processing on a background thread using something like NSOperation or an existing asynchronous API and update your view when you have finished.

Its hard to be of more help and get a better idea of whats going wrong without seeing your code unfortunately.

Luke Redpath
Hi, thanks for the response. I posted a bare-bones version of what I trying to do. I have a few questions though. 1) How come the main thread isn't blocked by addSubview such that [xmlParser parse] wouldn't be executed until after the view is loaded?2) Would it work/be-a-bad-idea to process data in the subviews viewDidLoad method?
moshe
A: 

Howdy! If you're looking for an easy work around:

[self showLoadingScreen]
[self performSelector:@selector(methodToDoWork) withObject:nil afterDelay:0.3];

However you're better off making methodToDoWork asynchronous if you can.

Tristan