简体   繁体   English

UIWebView - 在请求正在进行时解除模态视图控制器时崩溃

[英]UIWebView - crash when dismissing modal view controller while request is in progress

Hey all, I am presenting a modal view controller and loading a webpage on that view controller in a UIWebView: 嘿所有,我提出了一个模态视图控制器,并在UIWebView中的该视图控制器上加载一个网页:

- (void)viewWillAppear:(BOOL)animated
{
     self.requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:[MPServerPrefs serverPrefs].url_of_sandwich]];
     [self.helpWebView loadRequest:self.requestObj];
}

Everything works fine if I let the webpage load and then dismiss the view. 如果我让网页加载然后关闭视图,一切正常。 If I dismiss the view while the request is loading, I get this stacktrace: 如果我在加载请求时关闭视图,我会得到这个堆栈跟踪:

#0  0x31a94466 in objc_msgSend
#1  0x35ebcb70 in -[UIWebView webView:identifierForInitialRequest:fromDataSource:]
#2  0x35ebc1c0 in -[UIWebViewWebViewDelegate webView:identifierForInitialRequest:fromDataSource:]
#3  0x36130d04 in __invoking___
#4  0x36130bd4 in -[NSInvocation invoke]
#5  0x36130730 in -[NSInvocation invokeWithTarget:]
#6  0x329fc2f4 in -[_WebSafeForwarder forwardInvocation:]

I did some searching and can't figure out what's going on. 我做了一些搜索,无法弄清楚发生了什么。 Any ideas? 有任何想法吗? Do I need to cancel my request when dismissing the view controller? 在解雇视图控制器时是否需要取消我的请求?

Many thanks! 非常感谢!

Silly me - you just need to cancel the request and nil out the delegate! 傻傻的我 - 你只需要取消请求就可以取消代表!

[self.helpWebView setDelegate:nil];
[self.helpWebView stopLoading];

Your request is running on the main thread. 您的请求正在主线程上运行。 Does the request make your UI hang? 请求是否会使您的UI挂起? If you have to wait for the request to finish before the rest of the code executes, your user will be left wondering what's going on while the view is waiting to load. 如果您必须等待请求在其余代码执行之前完成,那么您的用户将不知道在视图等待加载时发生了什么。 Try running the request on a separate thread. 尝试在单独的线程上运行请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM