如何在不阻塞UI的情况下将内容加载到TableView中?

时间:2022-09-22 21:01:12

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

我正在开发一个TableView,控制器从web提要下载数据,解析并填充它在这个TableView中的内容。提要只提供10个条目的数据块。例如,当有112个条目时,加载数据需要大约12个请求到服务器。我希望在不阻塞用户屏幕的情况下发出这些请求,并且它应该按顺序加载数据,就像它不能在第5页上加载项一样,除非它已经按照这个示例的顺序获取了前一个(1,2,3,4)。

Any idea on how to implement this ?

你知道怎么实现这个吗?

Thx in advance for your help,

谢谢你的帮助,

Stephane

史蒂芬

2 个解决方案

#1


5  

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

使您的web调用异步。不要在主UI线程上进行web调用……

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 -

例如,如果您正在使用ASIHttp库进行http调用(它构建在苹果的NSURLConnection之上),那么生成异步请求就像-一样简单

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

这一定会让你的UI响应……

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

还要记住,只在主线程上更新UI元素。从后台线程开始更新ui元素很容易。所以请记住…

#2


1  

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.

您不需要使用另一个API,可以使用苹果自己的NSURLConnection。它可以同步或异步地检索数据。当然,后者对你来说是必要的。在请求的委托方法中保存数据。

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

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

另外,看看我最近对这个问题的更完整的回答。

#1


5  

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

使您的web调用异步。不要在主UI线程上进行web调用……

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 -

例如,如果您正在使用ASIHttp库进行http调用(它构建在苹果的NSURLConnection之上),那么生成异步请求就像-一样简单

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

这一定会让你的UI响应……

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

还要记住,只在主线程上更新UI元素。从后台线程开始更新ui元素很容易。所以请记住…

#2


1  

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.

您不需要使用另一个API,可以使用苹果自己的NSURLConnection。它可以同步或异步地检索数据。当然,后者对你来说是必要的。在请求的委托方法中保存数据。

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

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

另外,看看我最近对这个问题的更完整的回答。