Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView reloadData is slow

I have a function that connects to the internet and then refreshes the cells in the table.

My function is:

- (void) updateMethod
{

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    queue.name = @"Data request queue";

    [queue addOperationWithBlock:^{

        //neither of these delay the responsiveness of the table
        [columnArrayBackground removeAllObjects];
        [self getColumnDataBackground];

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            for (int i=0; i < 6; i++) {
                columnArray[i] = columnArrayBackground[i];
            };

            //THIS ONE LINE CAUSES DELAYS
            [homeTable reloadData];


        }];
    }];
}

Everything is super speedy EXCEPT for [homeTable reloadData]. When I comment that out, I have quick response. When I uncomment it, my cell response lags sometimes by a few seconds!

My other reloadData calls do not delay my app. Am I not implementing NSOperationQueue correctly?

like image 449
AllieCat Avatar asked Aug 29 '13 19:08

AllieCat


1 Answers

Remove the queues from your update method and leave only what updates the table. Under the action that refreshes your table add this code. Where the updateMethod is the code that updates the table. Only when you are back on the main thread do you reload the data. Hope this helps!

//perform on new thread to avoid the UI from freezing
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
               ^{
                   //background thread code
                   [self performSelector:@selector(updatemethod:) withObject:nil];
                   dispatch_async(dispatch_get_main_queue(),
                                  ^{    //back on main thread
                                      [self.tableView reloadData];
                                  });});
like image 93
Jeevan Thandi Avatar answered Oct 26 '22 15:10

Jeevan Thandi