Three20:取消网络请求
我正在使用 Three20 并实现像 TTRemoteExamples 示例一样的模型。现在的问题是:当我点击并打开一个页面时,发送了TTURLRequest,在从远程获取数据期间,我点击打开了另一个页面。但之前的网络请求仍然存在,弄乱了我加载的数据。所以我想知道当我切换到另一个页面时如何取消之前的网络请求。或者当我单击按钮在同一页面中执行新请求时。
谢谢~
I am using three20 and implement the model like the example of TTRemoteExamples. Now problem is: when I click and open a page, the TTURLRequest sent out, during fetch data from remote, I click to open another page. But the previous network request is still there messed my loaded data. So I want to know the way to cancel previous network request when I switch to another page. Or when I click button to do a new request in the same page.
Thanks~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要取消 TTURLRequest,请保留对其的引用(通常在实例变量中),然后向其发送“取消”消息。像这样:
如果您不希望委托被通知请求被取消,请执行以下操作:
您通常还希望在视图控制器的 dealloc 方法中执行此操作。如果在 viewController 被释放后请求继续,它将尝试向其发送委托消息,并且您将遇到严重的访问崩溃。
至于什么时候取消,就看你自己了。如果您需要它在用户离开视图控制器时停止,请实现 UIViewController 的 viewWillDisappear: 或 viewDidDisappear: 方法(不要忘记调用 super!)。
To cancel a TTURLRequest, keep a reference to it (typically in an instance variable) then send it a "cancel" message. Like so:
If you don't want the delegate to be notified of the request being cancelled, do:
You'll typically also want to do this in your view controller dealloc method. If a request continues after the viewController has been deallocated, it will try to send delegate messages to it, and you'll get a bad access crash.
As for the timing of when you cancel it, that's up to you. If you need it to stop when a user leaves your view controller, then implement UIViewController's viewWillDisappear: or viewDidDisappear: methods (don't forget to call super!).