 
         super.init()
     }
-
-    deinit {
-        taskMap.keys.forEach { (task) in
-            task.cancel()
-        }
-    }
 }
 
 // MARK: - WKURLSchemeHandler
 
 @available(iOS 11.0, *)
 extension WebKitImageDownloader: WKURLSchemeHandler {
+
     @available(iOS 11.0, *)
     public func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
         S1LogDebug("Start downloading \(urlSchemeTask.request)")
+
         var request = urlSchemeTask.request
+
         guard let urlString = request.url?.absoluteString else {
             urlSchemeTask.didFailWithError(WebKitImageDownloaderError.invalidURL)
             return
         }
+
         request.url = URL(string: urlString.s1_replace(pattern: "^image", with: "http"))
 
-        _start(schemeTask: urlSchemeTask, with: request)
+        let dataTask = self.session.dataTask(with: request)
+        self.taskMap[dataTask] = urlSchemeTask as Any
+        dataTask.resume()
     }
 
     @available(iOS 11.0, *)
     public func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
         S1LogDebug("Stop downloading \(urlSchemeTask.request)")
-        _stop(schemeTask: urlSchemeTask)
-    }
-}
 
-// MARK: - Helpers
-
-private extension WebKitImageDownloader {
-    @available(iOS 11.0, *)
-    func _start(schemeTask: WKURLSchemeTask, with request: URLRequest) {
-        delegateQueue.addOperation {
-            let dataTask = self.session.dataTask(with: request)
-            self.taskMap[dataTask] = schemeTask as Any
-            dataTask.resume()
-        }
-    }
-
-    @available(iOS 11.0, *)
-    func _stop(schemeTask: WKURLSchemeTask) {
-        /// We may performing data receive operation in delegate queue when this method called in main thread.
-        /// We suspend delegate queue to ensure all of our stop(schemeTask:) are called and handled before calling didReceive() method in background queue.
-        /// Even though delegateQueue.isSuspended is setted to ture, the task which already started when stop(schemeTask:) called will not be stopped.
-        /// So we catch exception throwed from WKWebView.
-
-        delegateQueue.addOperation {
-            for (theDataTask, theSchemeTask) in self.taskMap where (theSchemeTask as! WKURLSchemeTask) === schemeTask {
-                S1LogDebug("Cancel data task \(theDataTask.taskIdentifier).")
-                self.taskMap.removeValue(forKey: theDataTask)
-                theDataTask.cancel()
-                break
-            }
+        for (theDataTask, theSchemeTask) in self.taskMap where (theSchemeTask as! WKURLSchemeTask) === urlSchemeTask {
+            S1LogDebug("Cancel data task \(theDataTask.taskIdentifier).")
+            self.taskMap.removeValue(forKey: theDataTask)
+            theDataTask.cancel()
+            break
         }
     }
 }

//

import WebKit
import CocoaLumberjack

class WebKitImageDownloader: NSObject {
    let name: String

    private lazy var session = {

// MARK: - URLSessionDataDelegate

extension WebKitImageDownloader: URLSessionDataDelegate {

    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {

        if #available(iOS 11.0, *) {
            DispatchQueue.main.async {
                if let schemeTask = self.taskMap[dataTask] as? WKURLSchemeTask {
                    S1LogDebug("Task Receive Response \(schemeTask.request) \(dataTask.state == .running)")
                    schemeTask.didReceive(response)
                }
            }
        }

        completionHandler(.allow)
    }

    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {

        if #available(iOS 11.0, *) {
            DispatchQueue.main.async {
                if let schemeTask = self.taskMap[dataTask] as? WKURLSchemeTask {
                    S1LogVerbose("Task Receive Data \(schemeTask.request) Running: \(dataTask.state == .running)")
                    schemeTask.didReceive(data)
                }
            }
        }

    }

    func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
        if #available(iOS 11.0, *) {
            DispatchQueue.main.async {
                if let schemeTask = self.taskMap[task as! URLSessionDataTask] as? WKURLSchemeTask {
                    if let error = error {
                        S1LogWarn("Task Fail \(schemeTask.request) \(error)")
                        schemeTask.didFailWithError(error)
                    } else {
                        S1LogDebug("Task Finish \(schemeTask.request)")
                        schemeTask.didFinish()
                    }

                    self.taskMap.removeValue(forKey: task as! URLSessionDataTask)
                }
            }
        }
    }
