Reusing UIWebView is causing crashes

為{幸葍}努か 提交于 2019-11-30 14:09:49

I'm not certain this way is the "best" way to solve the issue, but it does seem to be working quite well. Short, sweet, and it works.

I disabled userInteraction with the tableView that updates the content in the webView. Since you have to mash on it so much, missing a tap here or there probably won't be missed. So for now, this is the fix.

#pragma mark -
#pragma mark UIWebViewDelegate methods

-(void)webViewDidStartLoad:(UIWebView *)webView {
    [self.tableView setUserInteractionEnabled:NO];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.tableView setUserInteractionEnabled:YES];
}

// I re-enable on load failures as they can block you out entirely on fail
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [self.tableView setUserInteractionEnabled:YES]; 
}

If it’s crashing only when you tap quickly, could you put a gesture recognizer over it to act as a poor man's rate limiter?

Are you using [webview stopLoading]; before loading another request? What you might need to do is cancel or stop the current loading before trying to load a different one. The other option being restrict the user input.

In your UIWebViewDelegate, you could implement webView:shouldStartLoadWithRequest:navigationType: to return NO if there is already a request loading.

I can't help but think you'd be better off not re-using the UIWebView. Ditch the nib, create it programmatically and set it to nil and re-create/re-assign/re-alloc it when the data source changes.

On a different note I would make sure to use NSOperationQueue for the data loading.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!