0

我有一个第三方 iOS 应用程序使用的 iOS SDK 项目,最近报告了围绕 SDK 中一个 API 的崩溃,原因是:

threading violation: expected the main thread [name NSInternalInconsistencyException, reason threading violation: expected the main thread]

第三方应用程序调用的 API 是异步的,它在后台线程中异步执行一些操作,并在主线程上向调用者返回一个回调。

我知道在后台线程而不是主线程中执行 UI 操作时经常会引发此异常。

但是,我不明白为什么仍然会发生这种情况,因为在该 SDK API 中,在将回调返回给调用者之前,我有以下代码来确保调用者在主线程上获得回调:

void runOnMainThread(void (^block)(void)){
if ([NSThread isMainThread]) {
    block();
} else {
    dispatch_async(dispatch_get_main_queue(), block);
}
}

堆栈跟踪:

Stack Trace : (
0   CoreFoundation                      0x00000001a154b9ec F3021642-E3C0-33F8-9911-DD303A6056D0 + 1157612
1   libobjc.A.dylib                     0x00000001b58ceb54 objc_exception_throw + 56
2   CoreFoundation                      0x00000001a145a50c F3021642-E3C0-33F8-9911-DD303A6056D0 + 169228
3   Foundation                          0x00000001a2754878 712A564E-5058-3EAC-AF3A-43BBD43D0D7E + 489592
4   FrontBoardServices                  0x00000001b08be614 E42FBC3B-619C-32AB-A210-4A25F8B5373F + 349716
5   FrontBoardServices                  0x00000001b08710a4 E42FBC3B-619C-32AB-A210-4A25F8B5373F + 32932
6   UIKitCore                           0x00000001a3485fdc CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 2088924
7   UIKitCore                           0x00000001a34847e8 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 2082792
8   UIKitCore                           0x00000001a348456c CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 2082156
9   UIKitCore                           0x00000001a401824c CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 14221900
10  UIKitCore                           0x00000001a3df3c44 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 11975748
11  UIKitCore                           0x00000001a3df4af0 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 11979504
12  UIKitCore                           0x00000001a32a37a8 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 112552
13  UIKitCore                           0x00000001a32a2728 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 108328
14  UIKitCore                           0x00000001a32a5a18 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 121368
15  third party app                     0x0000000106e81cc0 MySignalHandler + 184
16  libsystem_platform.dylib            0x00000001e90a9d90 3A71914A-C2A7-3514-B519-DF319E7A6E02 + 28048

调用者是否仍有可能在后台线程中获得回调?

4

0 回答 0