Why would delaying a thread response fix view corruption?

6 times out of 10 my very simple iPhone app is getting a corrupted display on launch or crashes randomly. But it behaves fine in the simulator. The display corruption looks like mis-colored fonts, out of place font text, wrong background colors, etc.

I've found a strange work-around.. when my thread delays by 2 seconds before calling the "done" notification, everything works swimmingly. The thread reads a web page and the "done" notification loads up a PickerView with strings. So what gives? Can I not safely initiate a threaded task from viewDidLoad?

- (void) loadWebPage:(NSString *)urlAddition {
      NSAutoreleasePool *subPool = [[NSAutoreleasePool alloc] init];
      NSString *pageSource;
      NSError *err;
      NSString *urlString = [NSString stringWithFormat:@"http://server/%@",
      pageSource = [NSString stringWithContentsOfURL:[NSURL URLWithString: urlString] 

      [NSThread sleepForTimeInterval:2.0]; // THIS STOPS THE DISPLAY CORRUPTION
      [[NSNotificationCenter defaultCenter] postNotificationName:@"webDoneNotification" 
      [subPool drain];

- (void) webDoneNotification: (NSNotification *)pNotification { 
      [mediaArray release];
      mediaArray = [[NSArray arrayWithObjects:
                     [NSString stringWithString:@"new pickerview text"], 
                     nil] retain];

      [mediaPickerView reloadAllComponents];

      [mediaPickerView selectRow:0 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
   mediaArray = [[NSArray arrayWithObjects:
                 [NSString stringWithString:@"init pickerview text"], 
                 nil] retain];

   if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization

    return self;

- (void)viewDidLoad {
   [super viewDidLoad];

   myWebThread = [[WebThread alloc] initWithDelegate:self];
   [[NSNotificationCenter defaultCenter] addObserver:self 
   [myWebThread performSelectorInBackground:@selector(loadWebPage:) withObject:@""];


Update: Even a delay of 0.1 seconds is enough to completely fix the problem.


You're updating the view from a background thread. This is what's causing your problems; UIKit views are not thread-safe. When you need to alter the view, do it in the main thread.

Need Your Help

Is there any way to read data from the django admin interface request for pre save validation?

python django validation django-models django-admin

I know the following function can be used to do some pre-save processing. But, how can I access the request data, for example a text field value from the request for doing validation?

SQL Server Pivot Table - Dynamic

sql-server pivot-table

I have a table, called #MonthlySales, that pulls up ItemNumber, MonYearNum & UnitsSold. I pivoted this with the following script: