Search code examples
iosuitableviewviewdidappear

iOS - viewDidAppear: delayed when showing a UITableView


I'm having a weird problem. I have a Tab Bar application, in it I'm using a Navigation Controller for each of the different tabs and each Navigation Controller has it's own View Controller to display some content.

When I touch a tab I want the tab to be loaded immediately with the view and after it has been loaded (viewDidAppear:) I want it to download some data in the viewDidAppear: method.

To test this functionality I have setup the following method. I'm using a NSThread to simulate doing some lengthy operation that should be done in the background while the view has been loaded. Then I assign the NSArray *test (nonatomic, retain) with some test data and reload the table.

I'm reloading the table because in the viewDidLoad method I'm setting the table to contain only one empty string (this is because I read that the table is setup before viewDidAppear: is called.

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

    DLog(@"viewDidAppear running");

    [NSThread sleepForTimeInterval:10];

    NSArray *array = [[NSArray alloc] initWithObjects:@"Test1", @"Test2", @"Test3", @"Test4", @"Test5", @"Test6", nil];
    self.test = array;

    [array release];

    [table reloadData];
}

So my expected behaviour should be that when touching this particular tab it should load instantly and then after 10 seconds it should be populated with table data from the viewDidAppear: method. But the behaviour is instead that when touching this tab it waits for 10 seconds and then it displays the view with the table data populated.


Solution

  • Calling [NSThread sleepForTimeInterval:10]; will prevent the main thread from doing any updates so you cannot see any user interface changes. If you want to delay reloading of table data as above, check NSObject performSelector:withObject:afterDelay: NSObject Class Reference

    Put the code for reloadData and array setup in a method an call it via performSelector:withObject:afterDelay: