UIViewController 从视图堆栈中弹出,NSURLConnection 使应用程序崩溃

发布于 2024-09-03 04:08:07 字数 496 浏览 6 评论 0原文

我正在将 UIViewController 推送到 UINavigationController 上。该视图控制器立即开始下载 xml feed,然后解析它。但是,如果您在下载完成之前点击后退按钮,则会因 EXC_BAD_ACCESS 而崩溃。导致它崩溃的行位于 parserDidEndDocument 中,是这一行:

if (self.delegate && [self.delegate conformsToProtocol:@protocol(ModelDelegate)]) [self.delegate modelDidFinishParsing:self];

我认为它正在崩溃,因为它正在尝试访问不再分配的 self.delegate 。我该如何解决这个问题?

另外,我会在 modelDidFinishParsing 方法中释放模型对象。如果这个模型从未达到这个方法,我将如何发布它。

I am pushing a UIViewController onto a UINavigationController. This view controller immediately starts a download of an xml feed and then parses it. However, if you hit the back button before it is done downloading, and crashes with EXC_BAD_ACCESS. The line that is crashing it is in parserDidEndDocument and is this line:

if (self.delegate && [self.delegate conformsToProtocol:@protocol(ModelDelegate)]) [self.delegate modelDidFinishParsing:self];

I assume it is crashing because it is trying to access self.delegate which is not assigned anymore. How do I get around this?

Also, I would release the model object in the modelDidFinishParsing method. How would I release this model if it never reaches this method.

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

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

发布评论

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

评论(2

孤单情人 2024-09-10 04:08:07

我在 AppDelegate 中设置对象来处理下载(以及其他异步或长时间运行的任务),然后根据不同控制器的需要触发它们。这样它们就被拥有并在应用程序的整个生命周期中具有持久性。

最好的方法是将它们传递给需要它们的 viewController(而不是 viewController“期望”appDelegate 准备好并等待这样一个对象)——依赖注入。

这些对象在完成时会以某种方式更新我的模型,如果需要,我会使用 NSNotifications 来宣布它们已完成。这使我摆脱了过去尝试取消或交换 viewWillDisappear 等中的委托以避免遇到的问题的麻烦。

I set up objects to handle my downloads (and other asynchronous or long running tasks) in the AppDelegate, then trigger them as required from various controllers. That way they are owned and have persistence through the life of the application.

The best way to do this is to pass them to the viewControllers that will need them (rather than the viewController "expecting" the appDelegate to have such and such an object ready and waiting) - dependency injection.

These objects update my model in some way when they finish and if I need to, I use NSNotifications to announce they are done. This isolates me from the mess I used to get into trying to cancel or swap delegates in viewWillDisappear etc to avoid the kind of issues you are running into.

羁客 2024-09-10 04:08:07

您的应用程序崩溃的原因可能是因为 NSURLConnection 保留了其委托(因此它可以可靠地回调它),但该委托具有弱引用的对象已被释放。

即,在您的情况下,当视图控制器弹出但 delegate 属性尚未清除(设置为 nil)时, self.delegate 指向的内容可能已被释放。

问题的解决方案是在 UIViewController 子类从导航堆栈中弹出时在适当的时间清除(nil) self.delegate 。

注意:保留委托对于 Cocoa 类来说并不常见。如果发生违反标准实践的情况,则会记录下来(请参阅 NSURLConnection 文档)。

The reason your app is crashing is probably because NSURLConnection retains its delegate (so it can call back to it reliably) but objects that this delegate has weak references to have been deallocated.

Ie, in your case what self.delegate points to has probably been deallocated when the view controller is popped but the delegate property has not been cleared (set to nil).

The solution to your problem is to clear (nil) self.delegate at the appropriate time when the UIViewController subclass is being popped off the navigation stack.

Note: retaining delegates is not usual behaviour for Cocoa classes. In situations where it happens contrary to standard practice it is documented (see the NSURLConnection docs).

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