iOS - another thread needs to send reloadData to the mainthread

痴心易碎 提交于 2019-11-27 06:14:01

问题


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


回答1:


[self.tableView performSelectorOnMainThread:@selector(reloadData)
                                 withObject:nil
                              waitUntilDone:NO];



回答2:


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];
});'



回答3:


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];
    }
}


来源:https://stackoverflow.com/questions/7914299/ios-another-thread-needs-to-send-reloaddata-to-the-mainthread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!