我正在开发一个 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
发布评论
评论(2)
使您的网络调用异步。不要在主 UI 线程上进行网络调用...
例如,如果您使用 ASIHttp 库 用于进行 http 调用(这是建立在 Apple 的 NSURLConnection),异步请求就像这样简单 -
当获取数据时,将调用这些选择器回调 -
这肯定会使您的 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 -
And when the data is fetched these selector callbacks are invoked -
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...
您不需要使用其他 API,可以使用 Apple 自己的
NSURLConnection
。它可以同步或异步检索数据。当然,后者对于您的情况是必要的。您将数据保存在请求的委托方法中。另外,请参阅我最近对 这个问题。
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.Also, see my recent more complete answer to this question.