views:

30

answers:

3

Hi all,

I have a UITableView which gets data from a server and updates in every 1 second(using performSelectorOnMainThread). Since this blocks main thread sometimes its not easy to scroll the table and its painfull for the user. Also i cant reduce my refresh interval also.

What are the possible solutions for this problem?

A: 

Hold your data in a mutable array or similar structure then asyncronously update that array with an NSURLConnection. You can call reloadData on the tableview to redraw the table when the NSURLConnection is done.

You would probably just call the NSUrlConnection from an NSTimer at whatever interval you prefer.

Ben
I already have the data fetching part.(from a socket connection)
charith
Excellent, then you just need to adjust how its stored locally and provide that info to the table.
Ben
+1  A: 

I would only refresh the visible cells as the data changes, and the others as they appear so it will be less consuming than updating the hole UITablaView

you can get the visible cells using ( from UITableView):

- (NSArray *)visibleCells

and you can update the remaining cells as they appear using UITableViewDelegate Protocol

– tableView:willDisplayCell:forRowAtIndexPath:

and i think this should make it a bit faster.

mklfarha
A: 

instead of calling performSelectorOnMainThread function of NSThread call

detachNewThreadSelector function. In this way your thread will not block the main thread

[NSThread detachNewThreadSelector:@selector(aMethod:) toTarget:[MyObject class] withObject:nil];

in toTarget: method you can write self instead of [MyObject class]

also in Implementation selector write @synchronize(self) for eg.

-(void)aMethod { @synchronize(self) {

     //write your whole code here 
  }

}

I have done the same thing in my application its work perfectly

Tarun Mittal
You cannot update the UI's other than the main thread.
charith
Yes It is true for that you have to manually reload the table after fetching all the data
Tarun Mittal