coolhongly
coolhongly

Reputation: 53

multithread weird delay in tableViewController

So in my tableViewController I put this code in. My idea is to use another thread called downloader to download some data which doesn't affect my main thread.

- (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;
}

However, there's some wired delay. In my log, "done" and "reloadDone?" appears immediately the moment I seague to my tableViewController, but "WT..?" appears like 6 secs later! That's so wired! Can Anyone help me out of this??

Upvotes: 0

Views: 75

Answers (1)

Jesse Gumpo
Jesse Gumpo

Reputation: 4797

any update to the UI should be done on the main queue

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

Upvotes: 1

Related Questions