views:

101

answers:

2

i have to reload uitableview simultaniously using a thread. i'm already using two threads for loading data from web. Is it possible using a thread for reloading tableview? Is there any other way?

A: 

simultaneously with what?

everything UIKit has to be done on the main thread, so you can use background threads to talk to the internet or to do your own processing, but any actual interaction with a UITableView has to be on the main thread.

David Maymudes
+1  A: 

Simultaneous to what? The reloading takes time and you need to reload the backing data model in background, while still displaying being able to display data to the user?

If that is the case, then I would:

  1. Define the data model as property.
  2. Update a temporary data model in a background thread.
  3. When updated I update the data model property on the main thread.

It is important to do the update of the real model property, and request of reloading data for the table view on the main thread. Otherwise there will be a time slot where the table view can request a view for a data model item that is no longer available.

An implementation would be something like this:

-(void)releadData;
{
  [self performSelectorInBackground:@selector(reloadDataInBackground)
                         withObject:nil];
}

-(void)reloadDataInBackground;
{
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  MyDataModel* model = nil;
  // Do what is needed to setup model.
  [self performSelectorOnMainThread:@selector(updateModelOnMainThread:)
                         withObject:model
                      waitUntilDone:NO];
  [pool release];
}

-(void) updateModelOnMainThread:(MyDataModel*)model;
{
  self.model = model;
  [self.tableView reloadData];
}
PeyloW