16
votes

Hey all, I am presenting a modal view controller and loading a webpage on that view controller in a 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!

2

2 Answers

42
votes

Silly me - you just need to cancel the request and nil out the delegate!

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

Your request is running on the main thread. Does the request make your UI hang? 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.