Вопрос

I have a table that is refreshing itself every two seconds. It works great on the simulator and on my wifi. But once I switch to the cellular network (or any slow network), I cannot select the rows reliably.

Sometimes when I click a row it will work after 8 seconds. Sometimes never.

I thought my refresh function was causing the delay but I printed the time at the beginning and end of the function and it only takes 2 milliseconds.

Has anyone had a similar slow network issue? Any tips on what might be the cause of the hang-up?

My refresh function is called in viewDidLoad:

//Set timer to call refresh function every two seconds
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(updateMethod) userInfo:nil repeats:YES];

My updateMethod is:

- (void) updateMethod
{
  [columnArray removeAllObjects];
  [self getColumnData];
  [homeTable reloadData];

}

getColumnData calls a website and puts data in the columnArray

Это было полезно?

Решение

You must not perform network operations on the main queue. You can create an NSOperationQueue to move the network logic to a background queue and only perform the UI update in the main queue when the network operation ends.

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

[queue addOperationWithBlock:^{
   [self getColumnData];
   [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [homeTable reloadData];
    }];
 }];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top