0

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

rmaddy
  • 314,917
  • 42
  • 532
  • 579
AllieCat
  • 3,890
  • 10
  • 31
  • 45

1 Answers1

2

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];
    }];
 }];
serrrgi
  • 644
  • 3
  • 7
  • I'm getting a SIGABRT error when I implement your code in my 'updateMethod'. Is that where this code should go? – AllieCat Aug 29 '13 at 16:41
  • The error reason: *** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]. I added "[columnArray removeAllObjects];" before [self getColumnData]. Was that incorrect? – AllieCat Aug 29 '13 at 16:49
  • Yes, you can create the queue in your updateMethod. Use zombies and create a breakpoint for all exceptions to find out about the error. – serrrgi Aug 29 '13 at 16:49
  • The for loop in getColumnData is no longer working quite right. The index goes: 0,1,2,3,4, 0, 5 – AllieCat Aug 29 '13 at 17:34
  • Thanks serrgi - I found and fixed the SIGABRT issue. This code fixed the performance issue! – AllieCat Aug 29 '13 at 17:49