开发者

How to load content into TableView without blocking the UI?

开发者 https://www.devze.com 2023-04-04 10:45 出处:网络
I\'m working on a TableView which controller downloads data from a web feed, parse开发者_如何学Python and populate its content in this TableView. The feed provides data in chunks of 10 items only. So,

I'm working on a TableView which controller downloads data from a web feed, parse开发者_如何学Python 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


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...


You do not need to use another API and can use Apple's own NSURLConnection. It can retrieve the data synchronously or asynchronously. Of course the latter is necessary in your case. You save the data in the requests's delegate methods.

– connection:didReceiveResponse:
– connection:didReceiveData:
– connection:didFailWithError:
– connectionDidFinishLoading:

Also, see my recent more complete answer to this question.

0

精彩评论

暂无评论...
验证码 换一张
取 消