views:

303

answers:

1

In my app when a user clicks a button I want to show a loading screen, then call a method that will load the data from the network and then load a view that displays the data in a UITableView. I have everything working except showing the loading screen. (UI hangs due to the fact that the network data/parsing logic is executing) Can anyone show me how to set the network business up to run in a separate thread and when I should initiate this thread.

I have a rootViewController which deals with the transitions. A subclasses UIViewController, UIView, and a UITableViewController to deal with this view.

A: 

Put the background code inside of a method like:

- (void)backgroundLogic;
{
    NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
    // do stuff here
    [ pool release ];
}

Then display the loading screen, and then start that work in a separate thread using

[NSThread detachNewThreadSelector: @selector(backgroundLogic) toTarget: self withObject: nil];

After the background thread is finished you can dismiss the loading screen and continue with the normal flow of the application.

Cosmin Stejerean
When do I detach the thread? in the UITableViewController's viewDidLoad?
FatAtomDev
Just to clarify the user will be looking at one view. They click a button. I load data from the network and call a parse function or two. Then I load a new view displaying the data in a tableView.
FatAtomDev
viewDidLoad seems like a reasonable place from which to detach the thread.
Cosmin Stejerean