如何停止失控的进程
因此,我正在开发这个为 iPhone 请求和检索 Web 服务内容的应用程序。我遇到的问题是:当我最初请求数据时,它会作为独立线程生成,以便应用程序不会因网络缓慢而变得无响应。这意味着,如果用户在数据下载完成之前离开当前页面,可能会发生意外的情况。
我设法将问题案例缩小到一个相对简单的案例:我有一些嵌套表,因此如果用户进入“消息”表(有时可能需要一段时间才能下载),然后立即退出,并且选择要查看的另一组消息,上一组消息最终会加载,因为它仍在队列中。
以下是我尝试过的操作:
1)我尝试取消操作,但这是徒劳的,因为由于我当时只允许队列中的一个操作,所以它会立即触发
2)我尝试验证数据的接收者是相同,但这不起作用,因为实际的表对象是两个选择之间的对象,它只需要不同的数据集。
有人对如何解决这个棘手的线程问题有任何一般性的编程建议吗?
关于 iPhone 的具体说明:如果我可以阻止用户退出消息表,我就不会有这个问题,因为他们基本上会被锁定在该视图中,直到数据加载完成。
谢谢!
So I'm working on this application that requests and retrieves webservice content for iPhone. The problem I am running into is this: When I initially request data, it is spawned off as an independent thread so that the application does not become unresponsive due to the network being slow. What this means is that if the user navigates away from the current page before this data finishes downloading, unexpected things can happen.
I have managed to narrow down the problem cases to one relatively simple one: I have some nested tables, so if a user goes down into the "Messages" table, which can sometimes take a little while to download, then back out immediately, and select a different set of messages to view, the previous set of messages ends up loading, because it was still in the queue.
Here are things I have tried:
1) I tried cancelling the operations, but this is futile, because since I only allow one operation in the queue at the time, it triggers immediately
2) I tried validating that the recipient of the data is the same, but this doesn't work because the actual table object is the between the two selections, it just needs a different data set.
Anyone have any general programming suggestions on how to solve this tricky threading problem?
On an iPhone specific note: if I could just stop the user from being able to back out of the messages table, I wouldn't have this problem, because they would basically be locked into that view until the data has finished loading.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这篇文章提供了一些与 iOS 相关的设计建议网络和线程。它的基本要点是“不要使用显式线程”,我完全同意。 NSURLConnection 具有出色的内置功能,可以从 URL 异步加载数据,同时为您管理所有线程。它们也可以随意取消。
如果您要使用 NSURLConnection 范例,那么当您退出请求视图控制器时,您可以简单地取消任何挂起的请求。
This post has some design advice relating to iOS networking and threading. The basic gist of it is "Don't use explicit threading", and I couldn't agree more. NSURLConnection has great built-in functionality for asynchronously loading data from a URL while managing all of the threading for you. They can also be cancelled easily at will.
If you were to use the NSURLConnection paradigm, you can simply cancel any pending request when you back out of the requesting view controller.