如何安全关闭加载UIWebView在viewWillDisappear? [英] How to safely shut down a loading UIWebView in viewWillDisappear?

查看:164
本文介绍了如何安全关闭加载UIWebView在viewWillDisappear?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图包含一个UIWebView加载谷歌地图(所以很多javascript等)。我的问题是,如果用户在网络视图完成加载之前点击导航栏上的后退按钮,我不清楚如何整洁地告诉网络视图停止加载,然后释放它,而没有得到消息发送到释放的实例。我也不确定网络视图喜欢它的容器视图消失之前,它完成(但我没有选择,如果用户在装载之前点击后退按钮)。

I have a view containing a UIWebView which is loading a google map (so lots of javascript etc). The problem I have is that if the user hits the 'back' button on the nav bar before the web view has finished loading, it is not clear to me how to tidily tell the web view to stop loading and then release it, without getting messages sent to the deallocated instance. I'm also not sure that a web view likes its container view disappearing before it's done (but I've no choice if the user hits the back button before it's loaded).

在我的viewWillDisappear处理程序我有这个

In my viewWillDisappear handler I have this

map.delegate=nil;
[self.map stopLoading];

这似乎可以处理大多数情况下,因为nil'ing委托停止发送didFailLoadWithError到我视图控制器。但是如果我在我的视图的dealloc方法中释放web视图,有时(间歇)我仍然会收到一条消息发送到deallocated实例,这似乎与javascript在实际页面中运行,例如:

this seems to handle most cases OK, as nil'ing the delegate stops it sending the didFailLoadWithError to my view controller. However if I release the web view in my view's dealloc method, sometimes (intermittently) I will still get a message sent to the deallocated instance, which seems to be related to the javascript running in the actual page, e.g.:

-[UIWebView webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:]: message sent to deallocated instance 0x4469ee0

如果我只是不发布webview,那么我不会得到这些消息,但我想我现在泄漏webview。

If I simply don't release the webview, then I don't get these messages though I guess I'm then leaking the webview.

如果我不发送'stopLoading'消息,只是在viewWillDisappear中释放webview,那么我看到这样的消息:

If I don't send the 'stopLoading' message, and simply release the webview within viewWillDisappear, then I see messages like this:

/SourceCache/WebCore/WebCore-351.9.42/wak/WKWindow.c:250 WKWindowIsSuspendedWindow:  NULL window.

可能相关,我有时(再次完全断断续续)得到一个丑陋的heisenbug,其他视图的navbar将弹出标题,但不是视图。换句话说,我得到左边的视图n的标题在堆栈,但视图显示仍然是视图n + 1(结果是你被困在这个屏幕上,不能回到根视图 - 你可以去其他方向,即推动更多的视图并弹回到没有弹出的视图,只是不是根视图。唯一的出路是退出应用程序)。在其他时候,在同一个视图上推动和弹出相同的序列工作正常。

Possibly related, I sometimes (again totally intermittent) get an ugly heisenbug where clicking the back button on some other view's navbar will pop the title, but not the view. In other words I get left with the title of view n on the stack, but the view showing is still view n+1 (the result is you're trapped on this screen and cannot get back to the root view - you can go the other direction, i.e. push more views and pop back to the view that didn't pop corrrectly, just not to the root view. The only way out is to quit the app). At other times the same sequence of pushes and pops on the same views works fine.

这个特别的一个驱动我坚果。我认为它可能与在视图加载之前消失的视图相关,即在这种情况下,我怀疑它可能在内存中乱写和混淆视图堆栈。或者,这可能是完全不相关的和其他地方的一个错误(我从来没有能够重现它在调试生成模式,它只发生在发布版本设置,当我不能用gdb :-)。从我的调试运行,我不认为我过度释放任何东西。我只是似乎能够触发它,如果在某一点,我已经打开了视图,有网络视图,并不会立即发生后。

This particular one is driving me nuts. I think it may be related to the view disappearing before the web view is loaded, i.e. in this case I suspect it may scribble on memory and confuse the view stack. Or, this could be completely unrelated and a bug somewhere else (i've never been able to reproduce it in debug build mode, it only happens with release build settings when I can't watch it with gdb :-). From my debug runs, I don't think I'm over-releasing anything. And I only seem to be able to trigger it if at some point I have hit the view that has the web view, and it doesn't happen immediately after that.

推荐答案

一个变体应该解决泄漏和僵尸问题:

A variation on this should fix both the leaking and zombie issues:

- (void)loadRequest:(NSURLRequest *)request
{
    [self retain];
    if ([webView isLoading])
        [webView stopLoading];
    [webView loadRequest:request];
    [self release];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self retain];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self release];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self release];
}

- (void)viewWillDisappear
{
    if ([webView isLoading])
        [webView stopLoading];
}

- (void)dealloc
{
    [webView setDelegate:nil];
    [webView release];
    [super dealloc];
}

这篇关于如何安全关闭加载UIWebView在viewWillDisappear?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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