【问题标题】:UITableView reloadData is slowUITableView reloadData 很慢
【发布时间】:2013-08-29 19:02:54
【问题描述】:

我有一个功能可以连接到互联网,然后刷新表格中的单元格。

我的功能是:

- (void) updateMethod
{

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    queue.name = @"Data request queue";

    [queue addOperationWithBlock:^{

        //neither of these delay the responsiveness of the table
        [columnArrayBackground removeAllObjects];
        [self getColumnDataBackground];

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            for (int i=0; i < 6; i++) {
                columnArray[i] = columnArrayBackground[i];
            };

            //THIS ONE LINE CAUSES DELAYS
            [homeTable reloadData];


        }];
    }];
}

除了 [homeTable reloadData] 之外,一切都非常快速。当我对此发表评论时,我会迅速做出回应。当我取消注释时,我的细胞反应有时会滞后几秒钟!

我的其他 reloadData 调用不会延迟我的应用程序。我没有正确实现 NSOperationQueue 吗?

【问题讨论】:

  • 您可以尝试仅重新加载可见单元格吗?如果您的数据源在用户向下滚动之前已更新,那么它可能会更快,例如 [homeTable reloadRowsAtIndexPaths:[homeTable indexPathsForVisibleRows]]
  • 显示表视图委托方法。 getColumnDataBackground 是做什么的?你怎么知道它导致了delay

标签: ios objective-c performance uitableview nsoperationqueue


【解决方案1】:

从更新方法中删除队列,只保留更新表的内容。在刷新表的操作下添加此代码。其中 updateMethod 是更新表的代码。只有当您回到主线程时,您才会重新加载数据。希望这可以帮助!

//perform on new thread to avoid the UI from freezing
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
               ^{
                   //background thread code
                   [self performSelector:@selector(updatemethod:) withObject:nil];
                   dispatch_async(dispatch_get_main_queue(),
                                  ^{    //back on main thread
                                      [self.tableView reloadData];
                                  });});

【讨论】:

  • 当后台线程和主线程同时访问和修改显示的数据时,此代码可能会导致竞争。
猜你喜欢
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-04
  • 1970-01-01
  • 2011-11-24
相关资源
最近更新 更多