美文网首页
关于WKWebview KVO的问题

关于WKWebview KVO的问题

作者: zgsddzwj | 来源:发表于2021-06-07 10:14 被阅读0次

    最近听云上监听到一个crash,内容如下:

    An instance 0x11c9d7400 of class WKWebView was deallocated while
     key value observers were still registered with it. 
    Current observation info: 
    <NSKeyValueObservationInfo 0x172a3b600> 
    ( <NSKeyValueObservance 0x170e498d0: 
    Observer: 0x177231b20, Key path: load)
    

    看一下崩溃路径,就是用户进入webView,然后点击返回按钮返回,就直接crash掉了。

    截屏2021-06-02 下午12.09.44.png

    但是我看到相应的VC中只有两个kvo,一个title的,一个estimatedProgress,根本就没有一个load相关的。然后我又搜遍了所有的webview相关的,也没有发现有一个load的监听。

    后来找到了一种方法,可以打印webView观察者相关的信息

                id info = self.webView.observationInfo;
                id info1 = [info valueForKey:@"_observances"];
                id info2 = [info1 valueForKey:@"_property"];
                id info3 = [info2 valueForKey:@"_keyPath"];
                NSLog(@"====== 0 %@",info);
                NSLog(@"====== 1 %@",info1);
                NSLog(@"====== 2 %@",info2);
                NSLog(@"====== 3 %@",info3);
    

    打印出来结果如下:

    2021-06-02 12:17:39.440592+0800 shansong[13307:3766327] ====== 0 <NSKeyValueObservationInfo 0x2807fa5a0> (
    <NSKeyValueObservance 0x281f0a070: Observer: 0x2807f01e0, Key path: loading, Options: <New: YES, Old: NO, Prior: NO> Context: 0x10b085000, Property: 0x281f09f20>
    <NSKeyValueObservance 0x281f0a190: Observer: 0x107d18240, Key path: title, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x281f0a160>
    <NSKeyValueObservance 0x281f0a2b0: Observer: 0x107d18240, Key path: estimatedProgress, Options: <New: NO, Old: NO, Prior: NO> Context: 0x105979f30, Property: 0x281f0a220>
    )
    2021-06-02 12:17:39.440724+0800 shansong[13307:3766327] ====== 1 (
        "<NSKeyValueObservance 0x281f0a070: Observer: 0x2807f01e0, Key path: loading, Options: <New: YES, Old: NO, Prior: NO> Context: 0x10b085000, Property: 0x281f09f20>",
        "<NSKeyValueObservance 0x281f0a190: Observer: 0x107d18240, Key path: title, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x281f0a160>",
        "<NSKeyValueObservance 0x281f0a2b0: Observer: 0x107d18240, Key path: estimatedProgress, Options: <New: NO, Old: NO, Prior: NO> Context: 0x105979f30, Property: 0x281f0a220>"
    )
    2021-06-02 12:17:39.440858+0800 shansong[13307:3766327] ====== 2 (
        "<NSKeyValueUnnestedProperty: Container class: DWKWebView, Key: loading, isa for autonotifying: NSKVONotifying_DWKWebView, Key paths of directly and indirectly affecting properties: none>",
        "<NSKeyValueUnnestedProperty: Container class: DWKWebView, Key: title, isa for autonotifying: NSKVONotifying_DWKWebView, Key paths of directly and indirectly affecting properties: none>",
        "<NSKeyValueUnnestedProperty: Container class: DWKWebView, Key: estimatedProgress, isa for autonotifying: NSKVONotifying_DWKWebView, Key paths of directly and indirectly affecting properties: none>"
    )
    2021-06-02 12:17:39.440908+0800 shansong[13307:3766327] ====== 3 (
        loading,
        title,
        estimatedProgress
    )
    

    然后发现这个loading的observer和我自己添加的两个不一样,于是查了下这个loading的class,

    (lldb) p ((id)0x2807f01e0)->isa
    (Class) $0 = NBSWKWebViewObserver
    

    发现这个罪魁祸首,NBSWKWebViewObserver,NBS相关的就是听云的,后来我把项目的听云sdk删掉再运行就没有这个可恶的loading了。

    后面,就是如何和听云相关的沟通了。

    相关文章

      网友评论

          本文标题:关于WKWebview KVO的问题

          本文链接:https://www.haomeiwen.com/subject/ljtosltx.html