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

发布于 2024-12-04 07:27:45 字数 262 浏览 0 评论 0 原文

我正在开发一个 TableView,该控制器从网络提要下载数据,解析并填充此 TableView 中的内容。该提要仅以 10 项为一组提供数据。因此,例如,当有 112 个项目时加载数据可能需要向服务器发出大约 12 个请求。我想在不阻塞用户屏幕的情况下发出这些请求,并且它应该按顺序加载数据,就像它无法加载第 5 页上的项目一样,除非它已经获取了前一个(1、2、3、4 按照这个确切的顺序)例子)。

关于如何实现这一点有什么想法吗?

提前感谢您的帮助,

斯蒂芬

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的年少不轻狂 2024-12-11 07:27:46

使您的网络调用异步。不要在主 UI 线程上进行网络调用...

例如,如果您使用 ASIHttp 库 用于进行 http 调用(这是建立在 Apple 的 NSURLConnection),异步请求就像这样简单 -

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

当获取数据时,将调用这些选择器回调 -

- (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];
}

这肯定会使您的 UI 响应...

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

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

秋千易 2024-12-11 07:27:46

您不需要使用其他 API,可以使用 Apple 自己的 NSURLConnection。它可以同步或异步检索数据。当然,后者对于您的情况是必要的。您将数据保存在请求的委托方法中。

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

另外,请参阅我最近对 ​​这个问题

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文