如何在异步 NSURLConnection 中处理 NSZombies?

发布于 2024-08-13 16:58:30 字数 289 浏览 6 评论 0原文

我目前正在将 NSURLConnection 与多个 UIViews 异步使用(每个视图都将 NSURLConnection 作为委托处理)。我遇到的问题是,当用户切换视图太快并且委托变成 NSZombie 时,应用程序崩溃 - 即 NSURLConnection 不再有活动委托。那么,第一个问题是是否有办法规避这个问题?

第二个问题很简单——我该如何处理 NSZombie?简单的 if(myObject != nil).. 根本不起作用。

I'm currently asynchronously using NSURLConnection with several UIViews (every view handles NSURLConnection as delegate). The problem I have is when user switches views too fast and the delegate becomes NSZombie the app crashes - that is NSURLConnection doesn't have living delegate anymore. So, the first question is if there's a way to circumvent this?

The second question is simple - how do I handle NSZombie? Simple if(myObject != nil).. doesn't work at all.

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

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

发布评论

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

评论(1

月寒剑心 2024-08-20 16:58:30

您需要 在处置其委托之前取消 NSURLConnection。只需在充当委托的 UIView 中保留对 NSURLConnection 的引用并调用 [urlConnection cancel] 即可。

释放消息后,如果继续使用该指针,则需要将指向该消息的指针设置为零。举个例子:

id myObject = [[SomeObject alloc] init];

/* Some code */

[myObject release];
myObject = nil;

/* Some more code */

if (myObject != nil) {
   [myObject doSomething];
}

但是请注意,向 nil 发送消息是有效的,因此您不需要保护消息发送。如果myObject == nil,它根本不会产生任何效果。

You need to cancel the NSURLConnection before you dispose it's delegate. Simply keep a reference to the NSURLConnection in your UIView that acts as a delegate and call [urlConnection cancel].

After you release a message you need to set your pointer to it to nil if you continue using that pointer. As an example:

id myObject = [[SomeObject alloc] init];

/* Some code */

[myObject release];
myObject = nil;

/* Some more code */

if (myObject != nil) {
   [myObject doSomething];
}

Notice however that it is valid to send a message to nil so you don't need to safe guard the message sending. It simply won't have any effect if myObject == nil.

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