Objective C- Trouble updating UI on main thread

I am having some trouble updating my UI using performSelectorOnMainThread. Here is my situation. In my viewDidLoad I set up an activity indicator and a label. Then I call a selector to retrieve some data from a server. Then I call a selector to update the UI after a delay. Here's the code:

- (void)viewDidLoad
    [super viewDidLoad];
    self.reloadSchools = [[UIAlertView alloc] init];
    self.reloadSchools.message = @"There was an error loading the schools. Please try again.";
    self.reloadSchools.title = @"We're Sorry";
    self.schoolPickerLabel = [[UILabel alloc]init];
    self.schoolPicker = [[UIPickerView alloc] init];
    self.schoolPicker.delegate = self;
    self.schoolPicker.dataSource = self;
    self.server = [[Server alloc]init];
    schoolList = NO;

    _activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:_activityIndicator];
    [self.view bringSubviewToFront:_activityIndicator];
    [_activityIndicator startAnimating];

    [NSThread detachNewThreadSelector: @selector(getSchoolList) toTarget: self withObject: nil];
    [self performSelector:@selector(updateUI) withObject:nil afterDelay:20.0];

The selector updateUI checks to see if the data was retrieved, and calls a selector on the main thread to update the UI accordingly. Here is the code for these parts:

    self.schools = [_server returnData];
    if(!(self.schools == nil)) {
        [self performSelectorOnMainThread:@selector(fillPickerView) withObject:nil waitUntilDone:YES];
    else {
        [self performSelectorOnMainThread:@selector(showError) withObject:nil waitUntilDone:YES];

-(void)showError {
    NSLog(@"show error");
    [_activityIndicator stopAnimating];
    [self.reloadSchools show];
-(void)fillPickerView {
     NSLog(@"fill picker view");
     schoolList = YES;
     NSString *schoolString = [[NSString alloc] initWithData:self.schools encoding:NSUTF8StringEncoding];
     self.schoolPickerLabel.text  = @"Please select your school:";
     self.shoolArray = [[schoolString componentsSeparatedByString:@"#"] mutableCopy];
     [self.schoolPicker reloadAllComponents];
     [_activityIndicator stopAnimating];

When the selector fillPickerView is called the activity indicator keeps spinning, the label text doesn't change, and the picker view doesn't reload its content. Can someone explain to me why the method I am using isn't working to update my ui on the main thread?


dispatch_async(dispatch_get_global_queue(0, 0), ^{
 //load your data here.
 dispatch_async(dispatch_get_main_queue(), ^{
                //update UI in main thread.

First of all you should not be using detachNewThreadSelector. You should use GCD and submit your background task to an async queue. Threads are costly to create. GCD does a much better job of managing system resources.

Ignoring that, your code doesn't make a lot of sense to me. You submit a method, getSchoolList, to run on a background thread. You don't show the code that you are running in the background.

Then use performSelector:withObject:afterDelay to run the method updateUI on the main thread after a fixed delay of 20 seconds.

updateUI checks for self.schools, which presumably was set up by your background thread, and may or may not be done. If self.schools IS nil, you call fillPickerView using performSelectorOnMainThread. That doesn't make sense because if self.schools is nil, there is no data to fill the picker.

If self.schools is not nil, you display an error, again using performSelectorOnMainThread.

It seems to me that the logic on your check of self.schools is backwards. If it is nil you should display an error and if it is NOT nil you should fill the picker.

Next problem: In both cases you're calling performSelectorOnMainThread:withObject:waitUntilDone: from the main thread. Calling that method from the main thread doesn't make sense.

Third problem: It doesn't make sense to wait an arbitrary amount of time for a background task to run to completion, and then either succeed or fail. You won't have any idea what's going on for the full 20 seconds. If the background task finishes sooner, you'll never know.

Instead, you should have your background task notify the main thread once the task is done. That would be a valid use of performSelectorOnMainThread:withObject:waitUntilDone:, while calling it from the main thread is not. (Again, though, you should refactor this code to use GCD, not using threads directly.

It seems pretty clear that you are in over your head. The code you posted needs to be rewritten completely.

Need Your Help

Are FP and OO orthogonal?

oop scala programming-languages functional-programming paradigms

I have heard this time and again, and I am trying to understand and validate the idea that FP and OO are orthogonal.

How to update bootstrap popover text?

javascript jquery twitter-bootstrap

I am using bootstrap-popover to show a message beside an element.