如何在异步 NSURLConnection 中处理 NSZombies?
我目前正在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要 在处置其委托之前取消
NSURLConnection
。只需在充当委托的UIView
中保留对 NSURLConnection 的引用并调用[urlConnection cancel]
即可。释放消息后,如果继续使用该指针,则需要将指向该消息的指针设置为零。举个例子:
但是请注意,向
nil
发送消息是有效的,因此您不需要保护消息发送。如果myObject == nil
,它根本不会产生任何效果。You need to cancel the
NSURLConnection
before you dispose it's delegate. Simply keep a reference to the NSURLConnection in yourUIView
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:
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 ifmyObject == nil
.