iOS:主线程检查器:在后台线程上调用的 UI API:-[UIView retainCount]

Posted

技术标签:

【中文标题】iOS:主线程检查器:在后台线程上调用的 UI API:-[UIView retainCount]【英文标题】:iOS: Main Thread Checker: UI API called on a background thread: -[UIView retainCount] 【发布时间】:2018-01-21 15:24:15 【问题描述】:

我有一个错误警告: 我通常在主线程上放置一个 distpach_async 以避免问题,但在我的代码中找不到函数 -[UIView retainCount]

Main Thread Checker: UI API called on a background thread: -[UIView retainCount]
PID: 71660, TID: 11215956, Thread name: (none), Queue name: com.apple.keyboard.xpc, QoS: 25
Backtrace:
4   liboainject.dylib                   0x0000000128d09ca4 _AllocationsObjcLogger + 74
5   liboainject.dylib                   0x0000000128d09e0e ___swapMethods_block_invoke_3 + 301
6   libsystem_blocks.dylib              0x0000000110e91bab _Block_object_assign + 207
7   UIKit                               0x000000010df0f4f6 __copy_helper_block_.2638 + 31
8   libsystem_blocks.dylib              0x0000000110e918f0 _Block_copy + 113
9   UIKit                               0x000000010e996dcc -[UIKeyboardTaskEntry initWithTask:] + 77
10  UIKit                               0x000000010e995a2c -[UIKeyboardTaskQueue performTaskOnMainThread:waitUntilDone:] + 119
11  UIKit                               0x000000010df0f498 __70-[UIKeyboardImpl syncInputManagerToKeyboardStateWithExecutionContext:]_block_invoke + 90
12  CoreFoundation                      0x000000010c4a336c __invoking___ + 140
13  CoreFoundation                      0x000000010c4a3240 -[NSInvocation invoke] + 320
14  Foundation                          0x000000010b5b85ce __NSXPCCONNECTION_IS_CALLING_OUT_TO_REPLY_BLOCK__ + 17
15  Foundation                          0x000000010b7e488a -[NSXPCConnection _decodeAndInvokeReplyBlockWithEvent:sequence:replyInfo:] + 790
16  Foundation                          0x000000010b7e89b0 __88-[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:]_block_invoke.346 + 505
17  libxpc.dylib                        0x000000011117c61c _xpc_connection_reply_callout + 36
18  libxpc.dylib                        0x00000001111719cc _xpc_connection_call_reply_async + 82
19  libdispatch.dylib                   0x0000000110da439f _dispatch_client_callout3 + 8
20  libdispatch.dylib                   0x0000000110dbc5b7 _dispatch_mach_msg_async_reply_invoke + 369
21  libdispatch.dylib                   0x0000000110dac57d _dispatch_queue_serial_drain + 434
22  libdispatch.dylib                   0x0000000110dad1ea _dispatch_queue_invoke + 336
23  libdispatch.dylib                   0x0000000110db0102 _dispatch_root_queue_drain + 772
24  libdispatch.dylib                   0x0000000110dafda0 _dispatch_worker_thread3 + 132
25  libsystem_pthread.dylib             0x000000011126f1ca _pthread_wqthread + 1387
26  libsystem_pthread.dylib             0x000000011126ec4d start_wqthread + 13
2018-01-21 15:51:21.776157+0100 Formbox_Renault_Showroom_Zoe Levallois[71660:11215956] [reports] Main Thread Checker: UI API called on a background thread: -[UIView retainCount]
PID: 71660, TID: 11215956, Thread name: (none), Queue name: com.apple.keyboard.xpc, QoS: 25
Backtrace:
4   liboainject.dylib                   0x0000000128d09ca4 _AllocationsObjcLogger + 74
5   liboainject.dylib                   0x0000000128d09e0e ___swapMethods_block_invoke_3 + 301
6   libsystem_blocks.dylib              0x0000000110e91bab _Block_object_assign + 207
7   UIKit                               0x000000010df0f4f6 __copy_helper_block_.2638 + 31
8   libsystem_blocks.dylib              0x0000000110e918f0 _Block_copy + 113
9   UIKit                               0x000000010e996dcc -[UIKeyboardTaskEntry initWithTask:] + 77
10  UIKit                               0x000000010e995a2c -[UIKeyboardTaskQueue performTaskOnMainThread:waitUntilDone:] + 119
11  UIKit                               0x000000010df0f498 __70-[UIKeyboardImpl syncInputManagerToKeyboardStateWithExecutionContext:]_block_invoke + 90
12  CoreFoundation                      0x000000010c4a336c __invoking___ + 140
13  CoreFoundation                      0x000000010c4a3240 -[NSInvocation invoke] + 320
14  Foundation                          0x000000010b5b85ce __NSXPCCONNECTION_IS_CALLING_OUT_TO_REPLY_BLOCK__ + 17
15  Foundation                          0x000000010b7e488a -[NSXPCConnection _decodeAndInvokeReplyBlockWithEvent:sequence:replyInfo:] + 790
16  Foundation                          0x000000010b7e89b0 __88-[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:]_block_invoke.346 + 505
17  libxpc.dylib                        0x000000011117c61c _xpc_connection_reply_callout + 36
18  libxpc.dylib                        0x00000001111719cc _xpc_connection_call_reply_async + 82
19  libdispatch.dylib                   0x0000000110da439f _dispatch_client_callout3 + 8
20  libdispatch.dylib                   0x0000000110dbc5b7 _dispatch_mach_msg_async_reply_invoke + 369
21  libdispatch.dylib                   0x0000000110dac57d _dispatch_queue_serial_drain + 434
22  libdispatch.dylib                   0x0000000110dad1ea _dispatch_queue_invoke + 336
23  libdispatch.dylib                   0x0000000110db0102 _dispatch_root_queue_drain + 772
24  libdispatch.dylib                   0x0000000110dafda0 _dispatch_worker_thread3 + 132
25  libsystem_pthread.dylib             0x000000011126f1ca _pthread_wqthread + 1387
26  libsystem_pthread.dylib             0x000000011126ec4d start_wqthread + 13  

你知道在哪里可以找到它,所以我可以使用:

 dispatch_async(dispatch_get_main_queue(), ^
            );

提前致谢。

【问题讨论】:

您发布的两个堆栈跟踪都从第 4 级开始。每个堆栈跟踪的前三个级别在哪里? 【参考方案1】:

您在主线程检查器的同时打开了一些内存管理调试工具。内存调试工具之一是调整保留/释放以进行一些额外的内存管理分析,该分析包括对 retainCount 的调用,UIView 需要从主线程调用。

即您正在使用两个彼此不兼容的工具。关掉一个。

另外,请通过http://bugreporter.apple.com 提交错误。开发人员工具应该阻止您使用此配置,或者应该提供更好的运行时诊断。

【讨论】:

以上是关于iOS:主线程检查器:在后台线程上调用的 UI API:-[UIView retainCount]的主要内容,如果未能解决你的问题,请参考以下文章

主线程检查器:在后台线程上调用的 UI API:-[UIApplication applicationState]

CoreMotion 显然是从后台线程调用 UI API

在 .Net Core 的另一个线程上调用函数

无法从 iOS6 中的后台线程调用主线程上的代码

iOS子线程操作UI

试图了解 iOS 中后台线程行为的幕后情况