iOS - another thread needs to send reloadData to the mainthread

前端 未结 3 455
星月不相逢
星月不相逢 2020-12-10 16:56

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 sepa

相关标签:
3条回答
  • 2020-12-10 17:14
    [self.tableView performSelectorOnMainThread:@selector(reloadData)
                                     withObject:nil
                                  waitUntilDone:NO];
    
    0 讨论(0)
  • 2020-12-10 17:18

    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];
        }
    }
    
    0 讨论(0)
  • 2020-12-10 17:30

    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];
    });'
    
    0 讨论(0)
提交回复
热议问题