Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView lazy loading optimization

I have UITableView. in tableView:cellForRow:atIndexPath: method (when data populates to cells) I've implemented some kind of lazy loading. If there's no object for key(key==row number) in rowData NSDictionary program launches requestDataForRow: method in background. so the data in cells gets populated a bit after the cell becomes visible. Here's the code:

static int requestCounter=0;

-(void)requestDataForRow:(NSNumber *)rowIndex
{
    requestCounter++;
    //NSLog(@"requestDataForRow: %i", [rowIndex intValue]);
    PipeListHeavyCellData *cellData=[Database pipeListHeavyCellDataWithJobID:self.jobID andDatabaseIndex:rowIndex];
    [rowData setObject:cellData forKey:[NSString stringWithFormat:@"%i", [rowIndex intValue]]];
    requestCounter--;

    NSLog(@"cellData.number: %@", cellData.number);

    if (requestCounter==0)
    {
        //NSLog(@"reloading pipe table view...");
        [self.pipeTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    };
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"pipeCellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    [[NSBundle mainBundle] loadNibNamed:@"PipesForJobCell" owner:self options:nil];
    cell = pipeCell;
    self.pipeCell = nil;


    PipeListHeavyCellData *cellData=[[PipeListHeavyCellData alloc] init];

    if ([rowData objectForKey:[NSString stringWithFormat:@"%i", indexPath.row]]==nil)
    {
        //NSLog(@"        nil data for row: %i", indexPath.row);
        [self performSelectorInBackground:@selector(requestDataForRow:) withObject:[NSNumber numberWithInt:indexPath.row]];
    }
    else
    {
        //NSLog(@"        has data for row: %i", indexPath.row);
        PipeListHeavyCellData *heavyData=[[PipeListHeavyCellData alloc] init];
        heavyData=(PipeListHeavyCellData *)[rowData objectForKey:[NSString stringWithFormat:@"%i", indexPath.row]];
        cellData._id=[heavyData._id copy];
        cellData.number=[heavyData.number copy];
        cellData.status=[heavyData.status copy];
};

This code works, everything is OK, BUT my table has 2000 rows and If users scrolls from cell with index 10 to cell with index 2000 very quickly. He must wait for a long time until all pulling data requests will complete (for rows 11, 12, 13, ..., 2000) cause that rows became visible while user was scrolling table view so the method requestDataForRow was called for them.

How can I optimize those things?

like image 841
user1385666 Avatar asked May 15 '12 14:05

user1385666


1 Answers

I had to do something similar. You'll need to create a a queue that processes the most recently added items first.

For example, the user opens the table and 10 requests are queued up. You dequeue the first object and start fetching the data for the first row. However, the user then scrolls down to rows 31-40. You'll then have to insert those rows before the first 10 in your queue because they are now higher priority. The key is that you don't immediately start 10 requests at once, but process them in order. That way, when the user scrolls, you only "waste" one request - the last request that was made.

An easy way to actually implement this is to use [tableView indexPathsForVisibleRows].

like image 77
Brandon O'Rourke Avatar answered Sep 30 '22 03:09

Brandon O'Rourke