5

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?

AllieCat
  • 3,890
  • 10
  • 31
  • 45
  • can you try to reload only the visible cells ? If your data source has been updated before the user scrolls down, then it might be faster i.e. something like [homeTable reloadRowsAtIndexPaths:[homeTable indexPathsForVisibleRows]] – Max Aug 29 '13 at 19:38
  • Show the table view delegate methods. What does `getColumnDataBackground` do ? How do you know it is causing a `delay` ? – Wain Aug 29 '13 at 19:41

1 Answers1

6

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];
                                  });});
Jeevan Thandi
  • 158
  • 2
  • 11
  • This code will likely cause a race, when background thread and main thread concurrently access and modify the data that's displayed. – CouchDeveloper Aug 29 '13 at 20:03