Iphone 重新加载数据前的延迟
我正在加载一个tableView,它从另一个位置填充它的一些元素。。。我正在使用NSNotificationCenter向视图控制器发送一条消息,以便在数据加载到数据源(NSMutableArray)后重新加载数据。。。我有一些NSLog来帮助调试,在触发通知和tableView实际重新加载数据之间有一个巨大的延迟。。。我想我已经追踪到了CellForRowatineXpath的一个延迟,它一点也不奇怪:Iphone 重新加载数据前的延迟,iphone,objective-c,nsnotifications,reloaddata,Iphone,Objective C,Nsnotifications,Reloaddata,我正在加载一个tableView,它从另一个位置填充它的一些元素。。。我正在使用NSNotificationCenter向视图控制器发送一条消息,以便在数据加载到数据源(NSMutableArray)后重新加载数据。。。我有一些NSLog来帮助调试,在触发通知和tableView实际重新加载数据之间有一个巨大的延迟。。。我想我已经追踪到了CellForRowatineXpath的一个延迟,它一点也不奇怪: - (UITableViewCell *)tableView:(UITableView *
- (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];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:@"updateRoot" object:nil];
在viewDidLoad方法中,我订阅通知:
- (void)updateView:(NSNotification *)notification {
NSLog(@"Notification Received, new count: %d", [theInvoice.lines count]);
[self.tableView reloadData];
}
[[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线程忙于做某事,而切换到主线程提高了速度吗?我在实现
tableView:heightforrowatinexpath:
delegate方法时遇到了类似的问题;不过,我的情况要复杂得多。你做过类似的事情吗?不,不使用heightForRowAtIndexPath:这是一张非常普通的桌子。。。正在加载的数据是xml,因此解析会有一个延迟,但该延迟正在完成,然后在表重新加载之前发送和接收通知。。。这是一个奇怪的暂停…您是否执行了任何可能阻塞主线程的处理,这可能会使它看起来很慢?您正在发送的通知-您是否有订阅同一通知的其他对象,这些对象可能正在处理任何内容?不,没有其他处理正在进行。。。我解析XML,发送通知。。。TableViewController处于空闲状态。。。收到通知后,有一个NSLog让我知道收到了通知以及NSMutableArray的新计数,然后调用tableView重载数据。。。然后是延迟,大约2秒,数据出现在表中……我认为您在主队列上使用dispatch_async是在转移注意力。这与您所做的基本相同,因为两者都在主线程上运行。您最初提到有2秒的延迟。然后您提到使用dispatch\u async大大减少了时间。我的直觉告诉我还有一些我们不知道的事情。