Search code examples
swiftuiprogressviewaddobserver

Swift class error WKWebView was deallocated while key value observers were still registered with it


I have a problem removing observer from my viewController named "myBrowser". I have a ProgressView bar, Here is the code where i add the observer:

 webView!.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)

in this controller i have a button that let me go back to another controller:

@IBAction func disa(sender: UIBarButtonItem) {

self.navigationController?.popViewControllerAnimated(true)

}

if i add

webView?.removeObserver(self, forKeyPath: "estimatedProgress")

inside disa func, my app crash because "WKWebView was deallocated while key value observers were still registered with it"...i tried put removeObserver in viewDidDisapper, in viewWillDisappear but my app still crash if i press the button before progressView bar is completely loaded.

How can i solve that problem?


Solution

  • Remove observer in the deinit method

    deinit {
        webView!.removeObserver(self, forKeyPath: "estimatedProgress")
    }