2012-07-13 112 views
0

所以在我的tableViewController我把這個代碼英寸我的想法是使用另一個線程稱爲下載器下載一些數據,這不影響我的主線程。多線程奇怪的延遲tableViewController

- (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?」當我將seague添加到我的tableViewController時,立即出現,但「WT ..?」看起來像6秒後!這是如此有線! 任何人都可以幫我解決這個問題嗎?

+0

做導演你http://stackoverflow.com/questions/11386403/delay-when-updating-view-in-the-completionhandler異步-http /請求/ 11386616#11386616 – 2012-07-13 19:33:15

回答

1

任何更新的用戶界面應該在主隊列

dispatch_async(downloadQueue, ^{ 
     self.bandList = [self.fetchData fetchBandList]; 
     NSLog(@"done"); 
     dispatch_async(dispatch_get_main_queue(),^{[self.tableView reloadData];}); 
     NSLog(@"reloadDone?"); 
    }); 
+0

哇這是快速的傢伙〜=) – coolhongly 2012-07-13 19:24:12