1

別の場所から要素の一部を取り込むtableViewをロードしています...データがデータソース(NSMutableArray)にロードされたら、NSNotificationCenterを使用してメッセージをView Controllerに送信し、reloadDataを送信しています...いくつかありますデバッグに役立つ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 スレッドが何かに引っかかっており、メイン スレッドに切り替えることで速度が向上したためでしょうか。

4

0 に答える 0