【问题标题】:iOS - another thread needs to send reloadData to the mainthreadiOS - 另一个线程需要将 reloadData 发送到主线程
【发布时间】:2011-12-16 09:22:13
【问题描述】:

我有一个单独的线程来创建一个 UIView 对象,将它插入 UITableView 的数据源,然后在 UITableView 上调用 reloadData。但是由于是单独的线程,所以不能直接调用reloadData,需要让主线程来做……但是怎么告诉主线程来做呢?

谢谢

【问题讨论】:

    标签: ios multithreading uitableview uiview reloaddata


    【解决方案1】:

    您可以使用异步调度,此方法允许您将工作线程编组为内联代码块,而不是使用 performSelectorOnMainThread 的方法:

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableview reloadData];
    });'
    

    【讨论】:

      【解决方案2】:

      这样的事情怎么样? (可能无法编译,我正在打出来)

      - (void) callReloadData
      {
          if ([NSThread isMainThread]) {
              @synchronized (self.tableView) {
                  [self.tableView reloadData];
              }
          } else {
              [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
          }
      }
      

      【讨论】:

      • 如果waitUntilDone: 为YES,performSelectorOnMainThread: 将立即调用其选择器,因此此代码毫无意义。 (我也不知道你为什么选择加入 @synchronized 块。)
      【解决方案3】:
      [self.tableView performSelectorOnMainThread:@selector(reloadData)
                                       withObject:nil
                                    waitUntilDone:NO];
      

      【讨论】:

      • 我是来复制这一行的 :) 。时长
      猜你喜欢
      • 2013-12-02
      • 1970-01-01
      • 2021-11-16
      • 2022-01-04
      • 2017-05-08
      • 2015-08-29
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多