Three20:取消网络请求

发布于 2024-12-04 11:18:31 字数 195 浏览 0 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(1

就是爱搞怪 2024-12-11 11:18:31

要取消 TTURLRequest,请保留对其的引用(通常在实例变量中),然后向其发送“取消”消息。像这样:

[self.myRequest cancel];

如果您不希望委托被通知请求被取消,请执行以下操作:

// I'm assuming self is the delegate here, that may not be true
[[self.myRequest delegates] removeObject:self];
[self.myRequest cancel];

您通常还希望在视图控制器的 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:

[self.myRequest cancel];

If you don't want the delegate to be notified of the request being cancelled, do:

// I'm assuming self is the delegate here, that may not be true
[[self.myRequest delegates] removeObject:self];
[self.myRequest cancel];

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

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