Наконец, вот решение, основанное на комментариях: Запустите поток в viewDidLoad, чтобы получить данные, не блокируя все:
- (void) viewDidLoad
{
dataLoaded = NO;
[self initSpinner];
[self launchLoadData];
...
}
-(void)launchLoadData {
NSLog(@"Launching thread");
[NSThread detachNewThreadSelector:@selector(loadData) toTarget:self withObject:nil];
}
- (void) loadData {
dataLoaded = NO;
NSLog(@" thread launched");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self loadDataFromURL:nil];
dataLoaded = YES;
[self.tableView reloadData];
[pool release];
}
- (void)loadDataFromURL:(NSString*)url {
// start the spinner to show that loading may be time consuming...
[NSThread detachNewThreadSelector: @selector(spinBegin) toTarget:self withObject:nil];
JSONLoader *loader = [[JSONLoader alloc] init];
self.accounts = [loader getAccountsFromURL:@"http://foo/bar/repository.json"];
[loader release];
//[NSThread sleepForTimeInterval:3];
[NSThread detachNewThreadSelector: @selector(spinEnd) toTarget:self withObject:nil];
}
и используйте флаг для отображения или отсутствия данных в таблице.tableView reloadData сделает все остальное при вызове из потока.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (dataLoaded) return [self.accounts count];
return 0;
}