7

I got a separate thread that creates a UIView object, inserts it into the UITableView's data source and then call reloadData on the UITableView. However, since it is a separate thread, it cannot call reloadData directly, it needs to make the mainthread do it... but how do you tell the mainthread to do it?

Thanks

KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199

3 Answers3

21
[self.tableView performSelectorOnMainThread:@selector(reloadData)
                                 withObject:nil
                              waitUntilDone:NO];
Can Berk Güder
  • 109,922
  • 25
  • 130
  • 137
7

You can use dispatch async, this method allows you to marshal worker thread to blocks of in-line code rather than methods using performSelectorOnMainThread:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableview reloadData];
});'
RichIntellect
  • 191
  • 2
  • 4
0

How about something like this? (may not compile, I am typing it out)

- (void) callReloadData
{
    if ([NSThread isMainThread]) {
        @synchronized (self.tableView) {
            [self.tableView reloadData];
        }
    } else {
        [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
    }
}
Mick Walker
  • 3,862
  • 6
  • 47
  • 72
  • 1
    `performSelectorOnMainThread:` will invoke its selector immediately if `waitUntilDone:` is YES, so this code pointless. (I have no idea why you chose to throw in a `@synchronized` block either.) – titaniumdecoy Nov 09 '12 at 18:05