All*_*Cat 5 performance objective-c uitableview nsoperationqueue ios
我有一个连接到互联网的功能,然后刷新表格中的单元格.
我的功能是:
- (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];
}];
}];
}
Run Code Online (Sandbox Code Playgroud)
除了[homeTable reloadData]之外,一切都超级快.当我发表评论时,我得到快速反应.当我取消注释时,我的细胞反应有时会滞后几秒钟!
我的其他reloadData调用不会延迟我的应用程序.我没有正确实现NSOperationQueue吗?
从更新方法中删除队列,只保留更新表的内容.在刷新表的操作下添加此代码.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];
});});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4582 次 |
| 最近记录: |