首页 > 解决方案 > iOS / MacOS 内容过滤 API WKContentRuleListStore compileContentRuleList 是否需要在主线程上调用?获取 EXC_BREAKPOINT

问题描述

在之前的 iOS 13 和 macOS Catalina 中,以下代码运行良好:

WKContentRuleListStore.default().compileContentRuleList(forIdentifier: "MYID", encodedContentRuleList: jsonFileContent) { (contentRuleList, error) in
    if let error = error {
        print("COMPILE ERROR: \(error)")
        return
    }
    if let list = contentRuleList {
        //print("GOT BLOCK LIST!")
        blockList = list
    }
}

但是在 iOS 14 和 Big Sur 中,这个崩溃EXC_BREAKPOINT也出现在另一个 SO 帖子中:

iOS 14 中的 WKContentRuleListStore.default() 崩溃

在 MacOS 上,它会崩溃:

Crashed Thread:        1  Dispatch queue: com.apple.NSURLSession-delegate

Exception Type:        EXC_BREAKPOINT (SIGTRAP)
Exception Codes:       0x0000000000000002, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Trace/BPT trap: 5
Termination Reason:    Namespace SIGNAL, Code 0x5
Terminating Process:   exc handler [2768]


Thread 1 Crashed:: Dispatch queue: com.apple.NSURLSession-delegate
0   com.apple.WebKit                0x00007fff3c5fc293 WTFCrashWithInfo(int, char const*, char const*, int) + 19
1   com.apple.WebKit                0x00007fff3c79aaa3 WebKit::runInitializationCode(void*) + 73
2   libc++.1.dylib                  0x00007fff20313e6a std::__1::__call_once(unsigned long volatile&, void*, void (*)(void*)) + 139
3   com.apple.WebKit                0x00007fff3c5a00fe WebKit::InitializeWebKit2() + 58
4   com.apple.WebKit                0x00007fff3c9be8e6 API::ContentRuleListStore::ContentRuleListStore(WTF::String const&, bool) + 44
5   com.apple.WebKit                0x00007fff3c9be83c API::ContentRuleListStore::ContentRuleListStore(bool) + 48
6   com.apple.WebKit                0x00007fff3c9be8a0 API::ContentRuleListStore::nonLegacyDefaultStore() + 60
7   com.apple.WebKit                0x00007fff3c831641 +[WKContentRuleListStore defaultStore] + 9
8   com.example.XXX             0x0000000107ebd2d0 closure #1 in getBlockList() + 1024 (AppDelegate.swift:1772)
9   com.example.XXX             0x0000000107ea3928 thunk for @escaping @callee_guaranteed (@guaranteed Data?, @guaranteed NSURLResponse?, @guaranteed Error?) -> () + 296
10  com.apple.CFNetwork             0x00007fff24780a5e 0x7fff246bc000 + 805470
11  com.apple.CFNetwork             0x00007fff246dae08 0x7fff246bc000 + 126472
12  libdispatch.dylib               0x00007fff201d25dd _dispatch_call_block_and_release + 12
13  libdispatch.dylib               0x00007fff201d37c7 _dispatch_client_callout + 8
14  libdispatch.dylib               0x00007fff201d95fe _dispatch_lane_serial_drain + 606
15  libdispatch.dylib               0x00007fff201da0fe _dispatch_lane_invoke + 426
16  libdispatch.dylib               0x00007fff201e3c5d _dispatch_workloop_worker_thread + 819
17  libsystem_pthread.dylib         0x00007fff2037b499 _pthread_wqthread + 314
18  libsystem_pthread.dylib         0x00007fff2037a467 start_wqthread + 15

在主线程上运行代码可以解决问题:

DispatchQueue.main.async {
    WKContentRuleListStore.default().compileContentRuleList(forIdentifier: "MYID", encodedContentRuleList: jsonFileContent) { (contentRuleList, error) in
        if let error = error {
            print("COMPILE ERROR: \(error)")
            return
        }
        if let list = contentRuleList {
            //print("GOT BLOCK LIST!")
            blockList = list
        }
    }
}

SDK 最近有变化吗?错误消息甚至没有像 Xcode 通常那样提及或给出任何主线程警告。而且我似乎找不到任何说明它必须在主线程上运行的文档。

标签: iosswiftmacoswkwebviewios14

解决方案


推荐阅读