重新加载数据之前的延迟
我正在加载一个 tableView,它从另一个位置填充它的一些元素...我正在使用 NSNotificationCenter 向视图控制器发送一条消息,以在数据加载到数据源(NSMutableArray)后重新加载数据...我有一些NSLogs 帮助调试,并且在触发通知和 tableView 实际重新加载数据之间存在巨大的延迟...我想我已经将其跟踪到 cellForRowAtIndexPath 中的延迟,它没有任何花哨的东西完全:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
Line *theLine = [theInvoice.lines objectAtIndex: indexPath.row];
cell.textLabel.text = theLine.name;
cell.detailTextLabel.text = [NSString stringWithFormat: @"qty: %@; unit-price: $%@", theLine.quantity, theLine.unit_price];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
return cell;
}
当收到通知时,我还有一个 NSLog 计数 NSMutableArray 数据源,并且它记录了正确数量的元素...然后在 tableView 重新加载之前有大约 2 秒的延迟。对于造成这种延迟的原因有什么想法吗?
编辑: 以下是接收通知的代码:
- (void)updateView:(NSNotification *)notification {
NSLog(@"Notification Received, new count: %d", [theInvoice.lines count]);
[self.tableView reloadData];
}
在 viewDidLoad 方法中,我订阅通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:@"updateRoot" object:nil];
更新: 我将 updateView 更改为这个,延迟显着减少:
- (void)updateView:(NSNotification *)notification {
NSLog(@"Notification Received, new count: %d", [theInvoice.lines count]);
//[self.tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self.tableView reloadData];
});
}
这是因为 UI 线程被捕获执行某些操作并切换到主线程提高了速度吗?
I am loading a tableView that populates some of it's elements from another location... I am using NSNotificationCenter to send a message to the view controller to reloadData once the data is loaded into the data source (NSMutableArray)... I have a few NSLogs to help with debugging and there is a huge delay between when the notification is fired and when the tableView actually reloads the data... I think I've tracked it down to a delay in cellForRowAtIndexPath which doesn't have anything fancy at all:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
Line *theLine = [theInvoice.lines objectAtIndex: indexPath.row];
cell.textLabel.text = theLine.name;
cell.detailTextLabel.text = [NSString stringWithFormat: @"qty: %@; unit-price: $%@", theLine.quantity, theLine.unit_price];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
return cell;
}
I also have an NSLog count the NSMutableArray data source when the notification is received and it logs the correct number of elements... then theres about a 2 second delay before the tableView reloads. Any thoughts on what could be the cause of this delay?
EDIT:
Here is the code for the receipt of the notification:
- (void)updateView:(NSNotification *)notification {
NSLog(@"Notification Received, new count: %d", [theInvoice.lines count]);
[self.tableView reloadData];
}
In the viewDidLoad method I am subscribing to the notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:@"updateRoot" object:nil];
UPDATE:
I changed my updateView to this and the delay decreased dramatically:
- (void)updateView:(NSNotification *)notification {
NSLog(@"Notification Received, new count: %d", [theInvoice.lines count]);
//[self.tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self.tableView reloadData];
});
}
Is this because the UI thread was caught up doing something and switching to the main thread increased the speed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论