Search code examples
objective-cioscocoa-touchuibuttonviewdidappear

Have buttons override other processes


In my app, the root view controller acquires information from the internet, parses it, and displays it in viewDidAppear. I am using this method because my app in embedded in a UINavigationController and this way the root view controller will reload its data when the user presses the back button and pops to the root view.

When this occurs, it takes some time for the information from the internet to be acquired and displayed. During this time, if the user clicks a button to move to a different view, the button action will not occur until the view controller has completed its process of acquiring the web data.

How can I make it so that the buttons will override the other processes and immediately switch the view? Is this safe? Thanks in advance.

Edit

Here's an example of a portion where I take the information off of the site (My app parses the HTML).

NSURL *siteURL = [NSURL URLWithString:@"http://www.ridgefield.org/ajax/dist/emergency-announcements"];
NSError *error;
NSString *source = [NSString stringWithContentsOfURL:siteURL 
                                            encoding:NSUTF8StringEncoding
                                               error:&error];

Solution

  • This is where Apple folks would hound, "don't block the main thread!".

    The primary suggestion for this kind of workflow is to use a separate thread (read: queue) for loading data from the web. Then the worker who completes the load can set some property on your view controller, and inside that setter is where the UI should be updated. Remember to call the setter back on the main thread.

    There are several ways to skin the concurrency cat, but the answer to this particular question leaves them out of scope. The short answer is to not do the load in the main thread, and that should lead you to the right place.