Resolve issue where we could have a web view deallocated before getting displayed.

This commit is contained in:
Maurice Parker 2020-09-01 18:54:46 -05:00
parent acaeb4164a
commit cf8dbb26f7
7 changed files with 94 additions and 91 deletions

View File

@ -13,7 +13,7 @@ import RSWeb
class PreloadedWebView: WKWebView {
private var isReady: Bool = false
private var readyCompletion: ((PreloadedWebView) -> Void)?
private var readyCompletion: (() -> Void)?
init(articleIconSchemeHandler: ArticleIconSchemeHandler) {
let preferences = WKPreferences()
@ -44,7 +44,7 @@ class PreloadedWebView: WKWebView {
loadFileURL(ArticleRenderer.blank.url, allowingReadAccessTo: ArticleRenderer.blank.baseURL)
}
func ready(completion: @escaping (PreloadedWebView) -> Void) {
func ready(completion: @escaping () -> Void) {
if isReady {
completeRequest(completion: completion)
} else {
@ -89,10 +89,10 @@ extension PreloadedWebView: WKNavigationDelegate {
private extension PreloadedWebView {
func completeRequest(completion: @escaping (PreloadedWebView) -> Void) {
func completeRequest(completion: @escaping () -> Void) {
isReady = false
navigationDelegate = nil
completion(self)
completion()
}
}

View File

@ -86,9 +86,7 @@ class WebViewProviderDequeueOperation: MainThreadOperation {
func run() {
if let webView = queue.lastObject as? PreloadedWebView {
webView.ready { preloadedWebView in
self.completion(preloadedWebView)
}
self.completion(webView)
self.queue.remove(webView)
self.operationDelegate?.operationDidComplete(self)
return
@ -98,9 +96,7 @@ class WebViewProviderDequeueOperation: MainThreadOperation {
let webView = PreloadedWebView(articleIconSchemeHandler: articleIconSchemeHandler)
webView.preload()
webView.ready { preloadedWebView in
self.completion(preloadedWebView)
}
self.completion(webView)
self.operationDelegate?.operationDidComplete(self)
}

View File

@ -452,6 +452,8 @@ private extension WebViewController {
sceneModel?.webViewProvider?.dequeueWebView() { webView in
webView.ready {
// Add the webview
webView.translatesAutoresizingMaskIntoConstraints = false
self.view.insertSubview(webView, at: 0)
@ -488,6 +490,8 @@ private extension WebViewController {
}
}
func renderPage(_ webView: PreloadedWebView?) {
guard let webView = webView else { return }

View File

@ -246,6 +246,8 @@ private extension WebViewController {
sceneModel?.webViewProvider?.dequeueWebView() { webView in
webView.ready {
// Add the webview
self.webView = webView
@ -269,6 +271,7 @@ private extension WebViewController {
self.renderPage(webView)
}
}
}

View File

@ -12,7 +12,7 @@ import WebKit
class PreloadedWebView: WKWebView {
private var isReady: Bool = false
private var readyCompletion: ((PreloadedWebView) -> Void)?
private var readyCompletion: (() -> Void)?
init(articleIconSchemeHandler: ArticleIconSchemeHandler) {
let preferences = WKPreferences()
@ -38,7 +38,7 @@ class PreloadedWebView: WKWebView {
loadFileURL(ArticleRenderer.blank.url, allowingReadAccessTo: ArticleRenderer.blank.baseURL)
}
func ready(completion: @escaping (PreloadedWebView) -> Void) {
func ready(completion: @escaping () -> Void) {
if isReady {
completeRequest(completion: completion)
} else {
@ -66,10 +66,10 @@ extension PreloadedWebView: WKNavigationDelegate {
private extension PreloadedWebView {
func completeRequest(completion: @escaping (PreloadedWebView) -> Void) {
func completeRequest(completion: @escaping () -> Void) {
isReady = false
navigationDelegate = nil
completion(self)
completion()
}
}

View File

@ -483,6 +483,8 @@ private extension WebViewController {
coordinator.webViewProvider.dequeueWebView() { webView in
webView.ready {
// Add the webview
webView.translatesAutoresizingMaskIntoConstraints = false
self.view.insertSubview(webView, at: 0)
@ -519,6 +521,8 @@ private extension WebViewController {
}
}
func renderPage(_ webView: PreloadedWebView?) {
guard let webView = webView else { return }

View File

@ -86,9 +86,7 @@ class WebViewProviderDequeueOperation: MainThreadOperation {
func run() {
if let webView = queue.lastObject as? PreloadedWebView {
webView.ready { preloadedWebView in
self.completion(preloadedWebView)
}
self.completion(webView)
self.queue.remove(webView)
self.operationDelegate?.operationDidComplete(self)
return
@ -98,9 +96,7 @@ class WebViewProviderDequeueOperation: MainThreadOperation {
let webView = PreloadedWebView(articleIconSchemeHandler: articleIconSchemeHandler)
webView.preload()
webView.ready { preloadedWebView in
self.completion(preloadedWebView)
}
self.completion(webView)
self.operationDelegate?.operationDidComplete(self)
}