NSURLConnection仍调用委托AFTER取消方法已被调用 [英] NSURLConnection still calls delegate AFTER cancel method has been called

查看:104
本文介绍了NSURLConnection仍调用委托AFTER取消方法已被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到NSURLConnection问题,如果我创建一个NSURLConnection并调用[connection connectionWithRequest]让它加载一点然后调用[连接取消]大部分时间工作正常。然而偶尔甚至在我调用[连接取消]之后,连接的委托仍然被调用(这会使应用程序崩溃)。谷歌搜索它看起来像这里的问题是在runloop中的竞争条件,我取消连接并释放委托但在runloop循环之前它调用委托函数 - >崩溃。

Having a problem with NSURLConnection, if I create a NSURLConnection and call [connection connectionWithRequest] let it load a little then call [connection cancel] most of the time that works fine. However occasionally even after I call [connection cancel] the connection's delegate still gets called (which crashes the app). Googling around it looks like the problem here is a race condition in the runloop, I cancel the connection and release the delegate but before the runloop cycles it calls the delegate functions -> crash.

在我打电话给[连接取消]确认连接已经取消后,我有办法吗?即使是糟糕的while()循环也可以:(

Is there a way for me to, after I call [connection cancel] confirm the connection has actually canceled? Even a crappy while() loop will do :(

推荐答案

在您的代表收到之前,您不应该释放连接和相关存储 connectionDidFinishLoading: connectionDidFailWithError:消息。

You should not release the connection & associated storage until your delegate receives either a connectionDidFinishLoading: or a connectionDidFailWithError: message.

代表通常不会被他们作为代理人的对象保留。但是在这种情况下,代表不应该在NSURLConnection仍然引用它时失效,除非你以某种方式过度释放它。

Delegates are not normally retained by the object they're acting as delegate for. However in this case it is, so the delegate should not become invalid while the NSURLConnection is still referring to it, unless you're over-releasing it somehow.

这篇关于NSURLConnection仍调用委托AFTER取消方法已被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆