Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load content into TableView without blocking the UI?

I'm working on a TableView which controller downloads data from a web feed, parse and populate its content in this TableView. The feed provides data in chunks of 10 items only. So, for example loading data when there are 112 items could require about 12 requests to server. I would like to make these requests without blocking user screen and it should load data in order, like it can't load items on page 5 unless it has already fetched previous one (1,2,3,4 in this exact order for the example).

Any idea on how to implement this ?

Thx in advance for your help,

Stephane

like image 927
Steve Avatar asked Feb 04 '26 13:02

Steve


1 Answers

Make your web calls asynchronous. Dont do web calls on the main UI thread...

For example if you are using ASIHttp library to make http calls (this is built on top of Apple's NSURLConnection), making an async request is as simple as -

NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];

And when the data is fetched these selector callbacks are invoked -

- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];

   // Use when fetching binary data
   NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}

This will definitely make your UI responsive...

Also keep in mind to update UI elements only on the main thread. It's easy to start updating ui elements from background threads. So keep in mind...

like image 53
Srikar Appalaraju Avatar answered Feb 07 '26 03:02

Srikar Appalaraju