iOS 10 - 在deinit上发生NSKeyValueObservation崩溃

时间:2018-04-27 08:44:42

标签: swift swift4 ios10 key-value-observing

我正在使用NSKeyValueObservation来观察WKWebView的子类中的属性。

它在iOS 11上运行良好,但在iOS 10上deinit崩溃。

控制台上的打印错误日志

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x15209e600 of class Rakuemon.WebView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x170232da0> (
<NSKeyValueObservance 0x170259bf0: Observer: 0x17027d500, Key path: loading, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x170643ba0>
<NSKeyValueObservance 0x170643480: Observer: 0x170c72f80, Key path: estimatedProgress, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x170643330>
<NSKeyValueObservance 0x170642c70: Observer: 0x17086c0c0, Key path: title, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x1706437b0>
)'

class WebView: WKWebView {

    // MARK: - Properties

    weak var delegate: WebViewDelegate?

    // MARK: - Private properties

    private var contentSizeObserver: NSKeyValueObservation?
    private var loadingObserver: NSKeyValueObservation?
    private var estimatedProgressObserver: NSKeyValueObservation?
    private var titleObserver: NSKeyValueObservation?

    // MARK: - Life cycle

    override init(frame: CGRect, configuration: WKWebViewConfiguration) {
        super.init(frame: frame, configuration: configuration)
        setupObserver()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - Private functions

private extension WebView {
    func setupObserver() {
        contentSizeObserver = scrollView.observe(\.contentSize, options: [.old, .new], changeHandler: { [unowned self] _, change in
            guard let oldSize = change.oldValue, let newSize = change.newValue, oldSize != newSize else { return }
            self.delegate?.webView?(self, didChangeSizeFrom: oldSize, to: newSize)
        })

        loadingObserver = observe(\.isLoading, changeHandler: { [unowned self] _, _ in
            self.delegate?.webViewIsLoading?(self)
        })

        estimatedProgressObserver = observe(\.estimatedProgress, options: [.new], changeHandler: { [unowned self] _, change in
            guard let newValue = change.newValue else { return }
            self.delegate?.webView?(self, didChangeEstimatedProgress: newValue)
        })

        titleObserver = observe(\.title, options: [.new], changeHandler: { [unowned self] _, change in
            guard let title = change.newValue else { return }
            self.delegate?.webView?(self, didChangeTitle: title ?? "")
        })
    }
}

问题

我还发现contentSizeObserverscrollView.contentSize,而不是self的属性,没有造成崩溃。

那么,通过self在iOS 10上观察NSKeyValueObservation属性的正确方法是什么?或取消注册?

3 个答案:

答案 0 :(得分:6)

我最终弄清楚如何在iOS 10上删除deinit中的观察者。

deinit {
    if let titleObserver = titleObserver {
        titleObserver.invalidate()
        removeObserver(titleObserver, forKeyPath: "title")
        self.titleObserver = nil
    }
}

答案 1 :(得分:2)

看起来像苹果虫 - https://bugs.swift.org/browse/SR-5816

  

这是因为NSKeyValueObservation持有对象的弱引用。那个弱的参考太快了。

在某些情况下,可以使用生命周期方法(viewDidDissapear等)找到解决方法。 在其他情况下,您应该使用旧的obj-c api(addObserver / removeObserver / observeValue)来支持iOS 10。

答案 2 :(得分:0)

这对我有用:

deinit {
    titleObserver = nil
}