美文网首页
猫猫学iOS之微博国际版的一个关于线程调用的异常修复Main T

猫猫学iOS之微博国际版的一个关于线程调用的异常修复Main T

作者: 翟乃玉 | 来源:发表于2021-03-12 12:21 被阅读0次

问题

优化微博国际版时候,发现控制台打出了这样的异常:

Main Thread Checker: UI API called on a background thread: -[UIApplication delegate]
PID: 5433, TID: 4812574, Thread name: (none), Queue name: com.apple.root.user-initiated-qos, QoS: 25
Backtrace:
4   WeiboOverseas                       0x00000001014bef6c -[*** layoutSpecThatFits:] + 3788
5   WeiboOverseas                       0x00000001022063d0 -[ASDisplayNode(ASLayoutSpec) _locked_layoutElementThatFits:] + 456
6   WeiboOverseas                       0x000000010221bcf0 __27+[ASDisplayNode initialize]_block_invoke + 192
7   WeiboOverseas                       0x0000000102205810 -[ASDisplayNode(ASLayoutSpec) calculateLayoutLayoutSpec:] + 588
8   WeiboOverseas                       0x0000000102222bcc -[ASDisplayNode calculateLayoutThatFits:] + 608
9   WeiboOverseas                       0x0000000102222830 -[ASDisplayNode calculateLayoutThatFits:restrictedToSize:relativeToParentSize:] + 364
10  WeiboOverseas                       0x00000001021fc988 -[ASDisplayNode(ASLayoutElement) layoutThatFits:parentSize:] + 1408
11  WeiboOverseas                       0x00000001021fc3fc -[ASDisplayNode(ASLayoutElement) layoutThatFits:] + 104
12  WeiboOverseas                       0x00000001021e75a4 -[ASDataController _layoutNode:withConstrainedSize:] + 576
13  WeiboOverseas                       0x00000001021e724c __47-[ASDataController _allocateNodesFromElements:]_block_invoke + 692
14  WeiboOverseas                       0x00000001021f6374 __ASDispatchApply_block_invoke + 144
15  libdispatch.dylib                   0x000000010575a338 _dispatch_call_block_and_release + 24
16  libdispatch.dylib                   0x000000010575b730 _dispatch_client_callout + 16
17  libdispatch.dylib                   0x000000010576cff8 _dispatch_root_queue_drain + 1020
18  libdispatch.dylib                   0x000000010576d698 _dispatch_worker_thread2 + 152
19  libsystem_pthread.dylib             0x0000000187447b38 _pthread_wqthread + 212
20  libsystem_pthread.dylib             0x000000018744a740 start_wqthread + 8
2021-03-12 12:06:18.059579+0800 WeiboOverseas[5433:4812574] [reports] Main Thread Checker: UI API called on a background thread: -[UIApplication delegate]
PID: 5433, TID: 4812574, Thread name: (none), Queue name: com.apple.root.user-initiated-qos, QoS: 25
Backtrace:
4   WeiboOverseas                       0x00000001014bef6c -[***** layoutSpecThatFits:] + 3788
5   WeiboOverseas                       0x00000001022063d0 -[ASDisplayNode(ASLayoutSpec) _locked_layoutElementThatFits:] + 456
6   WeiboOverseas                       0x000000010221bcf0 __27+[ASDisplayNode initialize]_block_invoke + 192
7   WeiboOverseas                       0x0000000102205810 -[ASDisplayNode(ASLayoutSpec) calculateLayoutLayoutSpec:] + 588
8   WeiboOverseas                       0x0000000102222bcc -[ASDisplayNode calculateLayoutThatFits:] + 608
9   WeiboOverseas                       0x0000000102222830 -[ASDisplayNode calculateLayoutThatFits:restrictedToSize:relativeToParentSize:] + 364
10  WeiboOverseas                       0x00000001021fc988 -[ASDisplayNode(ASLayoutElement) layoutThatFits:parentSize:] + 1408
11  WeiboOverseas                       0x00000001021fc3fc -[ASDisplayNode(ASLayoutElement) layoutThatFits:] + 104
12  WeiboOverseas                       0x00000001021e75a4 -[ASDataController _layoutNode:withConstrainedSize:] + 576
13  WeiboOverseas                       0x00000001021e724c __47-[ASDataController _allocateNodesFromElements:]_block_invoke + 692
14  WeiboOverseas                       0x00000001021f6374 __ASDispatchApply_block_invoke + 144
15  libdispatch.dylib                   0x000000010575a338 _dispatch_call_block_and_release + 24
16  libdispatch.dylib                   0x000000010575b730 _dispatch_client_callout + 16
17  libdispatch.dylib                   0x000000010576cff8 _dispatch_root_queue_drain + 1020
18  libdispatch.dylib                   0x000000010576d698 _dispatch_worker_thread2 + 152
19  libsystem_pthread.dylib             0x0000000187447b38 _pthread_wqthread + 212
20  libsystem_pthread.dylib             0x000000018744a740 start_wqthread + 8
2021-03-12 12:06:32.497324+0800 WeiboOverseas[5433:4812368] Could not signal service com.apple.WebKit.Networking: 113: Could not find specified service
=================================================================
Main Thread Checker: UI API called on a background thread: -[UIWindow safeAreaInsets]
PID: 5433, TID: 4812574, Thread name: (none), Queue name: com.apple.root.user-initiated-qos, QoS: 25
Backtrace:
4   WeiboOverseas                       0x00000001014befa4 -[**** layoutSpecThatFits:] + 3844
5   WeiboOverseas                       0x00000001022063d0 -[ASDisplayNode(ASLayoutSpec) _locked_layoutElementThatFits:] + 456
6   WeiboOverseas                       0x000000010221bcf0 __27+[ASDisplayNode initialize]_block_invoke + 192
7   WeiboOverseas                       0x0000000102205810 -[ASDisplayNode(ASLayoutSpec) calculateLayoutLayoutSpec:] + 588
8   WeiboOverseas                       0x0000000102222bcc -[ASDisplayNode calculateLayoutThatFits:] + 608
9   WeiboOverseas                       0x0000000102222830 -[ASDisplayNode calculateLayoutThatFits:restrictedToSize:relativeToParentSize:] + 364
10  WeiboOverseas                       0x00000001021fc988 -[ASDisplayNode(ASLayoutElement) layoutThatFits:parentSize:] + 1408
11  WeiboOverseas                       0x00000001021fc3fc -[ASDisplayNode(ASLayoutElement) layoutThatFits:] + 104
12  WeiboOverseas                       0x00000001021e75a4 -[ASDataController _layoutNode:withConstrainedSize:] + 576
13  WeiboOverseas                       0x00000001021e724c __47-[ASDataController _allocateNodesFromElements:]_block_invoke + 692
14  WeiboOverseas                       0x00000001021f6374 __ASDispatchApply_block_invoke + 144
15  libdispatch.dylib                   0x000000010575a338 _dispatch_call_block_and_release + 24
16  libdispatch.dylib                   0x000000010575b730 _dispatch_client_callout + 16
17  libdispatch.dylib                   0x000000010576cff8 _dispatch_root_queue_drain + 1020
18  libdispatch.dylib                   0x000000010576d698 _dispatch_worker_thread2 + 152
19  libsystem_pthread.dylib             0x0000000187447b38 _pthread_wqthread + 212
20  libsystem_pthread.dylib             0x000000018744a740 start_wqthread + 8
2021-03-12 12:06:32.542614+0800 WeiboOverseas[5433:4812574] [reports] Main Thread Checker: UI API called on a background thread: -[UIWindow safeAreaInsets]
PID: 5433, TID: 4812574, Thread name: (none), Queue name: com.apple.root.user-initiated-qos, QoS: 25
Backtrace:
4   WeiboOverseas                       0x00000001014befa4 -[**** layoutSpecThatFits:] + 3844
5   WeiboOverseas                       0x00000001022063d0 -[ASDisplayNode(ASLayoutSpec) _locked_layoutElementThatFits:] + 456
6   WeiboOverseas                       0x000000010221bcf0 __27+[ASDisplayNode initialize]_block_invoke + 192
7   WeiboOverseas                       0x0000000102205810 -[ASDisplayNode(ASLayoutSpec) calculateLayoutLayoutSpec:] + 588
8   WeiboOverseas                       0x0000000102222bcc -[ASDisplayNode calculateLayoutThatFits:] + 608
9   WeiboOverseas                       0x0000000102222830 -[ASDisplayNode calculateLayoutThatFits:restrictedToSize:relativeToParentSize:] + 364
10  WeiboOverseas                       0x00000001021fc988 -[ASDisplayNode(ASLayoutElement) layoutThatFits:parentSize:] + 1408
11  WeiboOverseas                       0x00000001021fc3fc -[ASDisplayNode(ASLayoutElement) layoutThatFits:] + 104
12  WeiboOverseas                       0x00000001021e75a4 -[ASDataController _layoutNode:withConstrainedSize:] + 576
13  WeiboOverseas                       0x00000001021e724c __47-[ASDataController _allocateNodesFromElements:]_block_invoke + 692
14  WeiboOverseas                       0x00000001021f6374 __ASDispatchApply_block_invoke + 144
15  libdispatch.dylib                   0x000000010575a338 _dispatch_call_block_and_release + 24
16  libdispatch.dylib                   0x000000010575b730 _dispatch_client_callout + 16
17  libdispatch.dylib                   0x000000010576cff8 _dispatch_root_queue_drain + 1020
18  libdispatch.dylib                   0x000000010576d698 _dispatch_worker_thread2 + 152
19  libsystem_pthread.dylib             0x0000000187447b38 _pthread_wqthread + 212
20  libsystem_pthread.dylib             0x000000018744a740 start_wqthread + 8

具体也是在这个时候,会有严重掉帧现象,通过调用栈看了一圈,找了一遍,整个人有点懵。。。

问题解决

  1. 其实异常第一行就告诉我们是哪里问题了,问题是子线程调用主线程的东东了,还告诉你调用的就是 -[UIApplication delegate] 和 -[UIWindow safeAreaInsets],这里我大约已经意识到问题在哪里了。
  2. 然后继续看调用栈 [**** layoutSpecThatFits:] + 3844 ,这里说明是在我们的layoutSpecThatFits方法里。 好了去找吧。
Main Thread Checker: UI API called on a background thread: -[UIApplication delegate]
Main Thread Checker: UI API called on a background thread: -[UIWindow safeAreaInsets]

最后发现,是因为这地方布局时候我们有做是否刘海屏的区分,刘海屏的判断代码我之前是这么写的:

var isIPhoneX: Bool {
    get {
        if #available(iOS 11.0, *), let window = UIApplication.shared.delegate?.window, let bottom = window?.safeAreaInsets.bottom {
            return bottom > 0
        }
        return false
    }
}

而这个计算属性用到的其他方法并不能保证任意线程调用 emmmm

这里我改成了static var, 然后再在app开始时 didFinishLaunchingWithOptions进行赋值,就可以解决这个问题了。

相关文章

网友评论

      本文标题:猫猫学iOS之微博国际版的一个关于线程调用的异常修复Main T

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