reloadData之前的延迟

时间:2011-12-29 00:15:15

标签: iphone objective-c nsnotifications reloaddata

我正在加载一个tableView,它从其他位置填充其中一些元素...我正在使用NSNotificationCenter在数据加载到数据源(NSMutableArray)后向视图控制器发送一条消息来重新加载数据...我有几个NSLog来帮助调试,并且在通知被触发和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线程遇到了某些事情并且切换到主线程增加了速度吗?

0 个答案:

没有答案