tableViewController中的多线程奇怪延迟

时间:2012-07-13 19:17:27

标签: iphone objective-c ios multithreading uitableview

所以在我的tableViewController中我把这段代码放入。我的想法是使用另一个名为downloader的线程来下载一些不影响我主线程的数据。

- (void)viewDidLoad
{
    [super viewDidLoad];
    dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
    dispatch_async(downloadQueue, ^{
        self.bandList = [self.fetchData fetchBandList];
        NSLog(@"done");
        [self.tableView reloadData];
        NSLog(@"reloadDone?");
    });
    dispatch_release(downloadQueue);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"WT..?");
    static NSString *CellIdentifier = @"Band List";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    cell.textLabel.text = [[self.bandList objectAtIndex:indexPath.row] objectForKey:@"itemname"];

    return cell;
}

然而,有一些有线延迟。 在我的日志中,"完成"和" reloadDone?"我一直看到我的tableViewController,但是" WT ..?"看起来像6秒后!这很有线! 任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

应该在主队列

上对UI进行任何更新
dispatch_async(downloadQueue, ^{
        self.bandList = [self.fetchData fetchBandList];
        NSLog(@"done");
        dispatch_async(dispatch_get_main_queue(),^{[self.tableView reloadData];});
        NSLog(@"reloadDone?");
    });