ARKit -[UIView setAnimationsEnabled:] 不支持从后台线程对 UIView 或子类执行任何操作
ARKit -[UIView setAnimationsEnabled:] Performing any operation from a background thread on UIView or a subclass is not supported
我有一个 ViewController,里面有一个 collectionView。我在 ARSCNView 中显示 collectionView 单元格的内容。
当我将 ViewController 的视图 属性 设置为 Material 的 content property
时,我从问题中得到了错误。
material.diffuse.contents = self.myViewController.view // produces error and sometimes it crashes. When it does't crash I can see the collectionView fine but the error is still there
我知道是 vc 的 view property
导致了问题,因为当我尝试设置 vc 而不使用它的视图 属性 时出现错误离开。问题是我再也看不到场景中的 vc。
material.diffuse.contents = self.myViewController // produces no error but the collectionView is no longer visible.
如何在 mainQueue 上设置 vc 的视图 属性 来避免这个问题?
我的代码(除了委托方法,这是 class 中的所有代码):
MyViewController: UIViewController, ...CollectionViewDataSource && Delegate {
lazy var collectionView: UICollectionView = {
// create collectionView
}()
var dataSource = ["0", "1", "2", "3", "4" ...]
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.view.backgroundColor = UIColor.clear
self.view.addSubview(collectionView)
// set its anchors
}
}
}
ClassWithSceneKit:
lazy var sceneView: ARSCNView = { ... }()
let myVC = MyViewController()
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.myVC.tableData.append("9")
self.myVC.tableData.append("10")
self.myVC.tableData.append("11")
self.myVC.collectionView.reloadData()
self.myVC.view.isOpaque = false // the background is clear, look in the picture I attached
let material = SCNMaterial()
material.diffuse.contents = self.myVC.view // ** this is the line causing the problem ***
let plane = SCNPlane(width: 0.5, height: 0.7)
plane.materials = [material]
plane.cornerRadius = 0.015
let node = SCNNode()
node.geometry = plane
node.position = SCNVector3(0.36, 0.12, -0.8)
self.sceneView.scene.rootNode.addChildNode(node)
}
}
ARKit_4[20429:4746699] [Animation] +[UIView setAnimationsEnabled:]
being called from a background thread. Performing any operation from a
background thread on UIView or a subclass is not supported and may
result in unexpected and insidious behavior.
trace=(
0 UIKitCore 0x000000019a855fa4 B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 15241124
1 libdispatch.dylib 0x0000000103356bd8 _dispatch_client_callout + 16
2 libdispatch.dylib 0x00000001033584c8 _dispatch_once_callout + 84
3 UIKitCore 0x000000019a855f08 B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 15240968
4 UIKitCore 0x000000019a856090 B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 15241360
5 UIKitCore 0x0000000199b9d030 B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 1900592
6 UIKitCore 0x000000019a5fdd0c B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 12782860
7 UIKitCore 0x000000019a40462c B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 10712620
8 UIKitCore 0x000000019a5fbaa4 B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 12774052
9 UIKitCore 0x000000019a401b8c B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 10701708
10 UIKitCore 0x000000019a4029ac B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 10705324
11 UIKitCore 0x000000019a402710 B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 10704656
12 SceneKit 0x00000001aa496864 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 2283620
13 SceneKit 0x00000001aa4969c8 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 2283976
14 SceneKit 0x00000001aa4b67d8 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 2414552
15 SceneKit 0x00000001aa329168 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 786792
16 SceneKit 0x00000001aa2f94c8 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 591048
17 SceneKit 0x00000001aa2f93f0 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 590832
18 SceneKit 0x00000001aa31c0f0 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 733424
19 SceneKit 0x00000001aa31935c 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 721756
20 SceneKit 0x00000001aa31809c 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 716956
21 SceneKit 0x00000001aa31700c 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 712716
22 SceneKit 0x00000001aa43f878 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1927288
23 SceneKit 0x00000001aa43f5cc 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1926604
24 SceneKit 0x00000001aa44684c 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1955916
25 SceneKit 0x00000001aa2d2554 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 431444
26 SceneKit 0x00000001aa2d3ea4 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 437924
27 SceneKit 0x00000001aa3cbe28 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1453608
28 SceneKit 0x00000001aa3cc694 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1455764
29 SceneKit 0x00000001aa3ccc00 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1457152
30 SceneKit 0x00000001aa3ccf94 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1458068
31 SceneKit 0x00000001aa4672f0 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 2089712
32 ARKit 0x00000001b176d104 4003283E-C369-3DBD-A6D1-3D04690F6674 + 958724
33 SceneKit 0x00000001aa32d8d0 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 805072
34 SceneKit 0x00000001aa432b1c 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1874716
35 libdispatch.dylib 0x0000000103356bd8 _dispatch_client_callout + 16
36 libdispatch.dylib 0x0000000103365858 _dispatch_lane_barrier_sync_invoke_and_complete + 124
37 SceneKit 0x00000001aa432aac 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1874604
38 GPUToolsCore 0x000000010357d5f8 -[DYDisplayLinkInterposer forwardDisplayLinkCallback:] + 168
39 QuartzCore 0x000000019cc72860 8705A7FE-6FD6-301F-BE30-D32F13CB6C0E + 71776
40 IOKit 0x00000001972a4930 IODispatchCalloutFromCFMessage + 488
41 CoreFoundation 0x00000001962ba670 FF63481D-FB6A-353B-B2C1-AC4EAECF594D + 525936
42 CoreFoundation 0x00000001962e3e40 FF63481D-FB6A-353B-B2C1-AC4EAECF594D + 695872
43 CoreFoundation 0x00000001962e356c FF63481D-FB6A-353B-B2C1-AC4EAECF594D + 693612
44 CoreFoundation 0x00000001962de440 FF63481D-FB6A-353B-B2C1-AC4EAECF594D + 672832
45 CoreFoundation 0x00000001962dd8a0 CFRunLoopRunSpecific + 464
46 Foundation 0x000000019661d824 3C682044-3249-3938-8111-F2F12D066D2B + 30756
47 SceneKit 0x00000001aa32dcac 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 806060
48 SceneKit 0x00000001aa32def4 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 806644
49 libsystem_pthread.dylib 0x00000001960821d0 _pthread_start + 124
50 libsystem_pthread.dylib 0x0000000196085ae0 thread_start + 8
)
我根据 Playing GIF using GIFU library in SceneKit causes app UI freeze any solution? UIView Animated being called from a background thread 使用 view.layer
而不是 view
解决了类似的问题:
material.diffuse.contents = self.myVC.view.layer
我联系了 Apple,他们说目前不支持此功能。他们根据保密协议向我发送了信息,因此我无法详细说明。
他们确实说过,如果这是我想要的功能,我可以向他们的团队发送请求,看看他们是否会考虑添加它。
当我将 SwiftUI view
包装到 HostingViewController
并将其传递给 SCNNode.material.diffuse.contents
时,我遇到了非常相似的问题。问题是,在 iOS15 之后,只显示视图的第一个 快照 - 没有动画,没有交互。
我尝试了很多解决方法,例如每秒将 view
渲染到 Image
30 次并将其传递给 SCNNode
,但没有任何效果。
我最终计算了透视变换矩阵并在 view
上应用了 CATransform3D
。
现在很有魅力。
我有一个 ViewController,里面有一个 collectionView。我在 ARSCNView 中显示 collectionView 单元格的内容。
当我将 ViewController 的视图 属性 设置为 Material 的 content property
时,我从问题中得到了错误。
material.diffuse.contents = self.myViewController.view // produces error and sometimes it crashes. When it does't crash I can see the collectionView fine but the error is still there
我知道是 vc 的 view property
导致了问题,因为当我尝试设置 vc 而不使用它的视图 属性 时出现错误离开。问题是我再也看不到场景中的 vc。
material.diffuse.contents = self.myViewController // produces no error but the collectionView is no longer visible.
如何在 mainQueue 上设置 vc 的视图 属性 来避免这个问题?
我的代码(除了委托方法,这是 class 中的所有代码):
MyViewController: UIViewController, ...CollectionViewDataSource && Delegate {
lazy var collectionView: UICollectionView = {
// create collectionView
}()
var dataSource = ["0", "1", "2", "3", "4" ...]
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.view.backgroundColor = UIColor.clear
self.view.addSubview(collectionView)
// set its anchors
}
}
}
ClassWithSceneKit:
lazy var sceneView: ARSCNView = { ... }()
let myVC = MyViewController()
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.myVC.tableData.append("9")
self.myVC.tableData.append("10")
self.myVC.tableData.append("11")
self.myVC.collectionView.reloadData()
self.myVC.view.isOpaque = false // the background is clear, look in the picture I attached
let material = SCNMaterial()
material.diffuse.contents = self.myVC.view // ** this is the line causing the problem ***
let plane = SCNPlane(width: 0.5, height: 0.7)
plane.materials = [material]
plane.cornerRadius = 0.015
let node = SCNNode()
node.geometry = plane
node.position = SCNVector3(0.36, 0.12, -0.8)
self.sceneView.scene.rootNode.addChildNode(node)
}
}
ARKit_4[20429:4746699] [Animation] +[UIView setAnimationsEnabled:] being called from a background thread. Performing any operation from a background thread on UIView or a subclass is not supported and may result in unexpected and insidious behavior.
trace=(
0 UIKitCore 0x000000019a855fa4 B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 15241124
1 libdispatch.dylib 0x0000000103356bd8 _dispatch_client_callout + 16
2 libdispatch.dylib 0x00000001033584c8 _dispatch_once_callout + 84
3 UIKitCore 0x000000019a855f08 B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 15240968
4 UIKitCore 0x000000019a856090 B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 15241360
5 UIKitCore 0x0000000199b9d030 B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 1900592
6 UIKitCore 0x000000019a5fdd0c B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 12782860
7 UIKitCore 0x000000019a40462c B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 10712620
8 UIKitCore 0x000000019a5fbaa4 B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 12774052
9 UIKitCore 0x000000019a401b8c B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 10701708
10 UIKitCore 0x000000019a4029ac B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 10705324
11 UIKitCore 0x000000019a402710 B7E79AF1-F09F-3BCE-B073-ECEA027BC0E8 + 10704656
12 SceneKit 0x00000001aa496864 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 2283620
13 SceneKit 0x00000001aa4969c8 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 2283976
14 SceneKit 0x00000001aa4b67d8 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 2414552
15 SceneKit 0x00000001aa329168 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 786792
16 SceneKit 0x00000001aa2f94c8 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 591048
17 SceneKit 0x00000001aa2f93f0 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 590832
18 SceneKit 0x00000001aa31c0f0 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 733424
19 SceneKit 0x00000001aa31935c 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 721756
20 SceneKit 0x00000001aa31809c 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 716956
21 SceneKit 0x00000001aa31700c 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 712716
22 SceneKit 0x00000001aa43f878 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1927288
23 SceneKit 0x00000001aa43f5cc 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1926604
24 SceneKit 0x00000001aa44684c 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1955916
25 SceneKit 0x00000001aa2d2554 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 431444
26 SceneKit 0x00000001aa2d3ea4 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 437924
27 SceneKit 0x00000001aa3cbe28 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1453608
28 SceneKit 0x00000001aa3cc694 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1455764
29 SceneKit 0x00000001aa3ccc00 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1457152
30 SceneKit 0x00000001aa3ccf94 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1458068
31 SceneKit 0x00000001aa4672f0 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 2089712
32 ARKit 0x00000001b176d104 4003283E-C369-3DBD-A6D1-3D04690F6674 + 958724
33 SceneKit 0x00000001aa32d8d0 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 805072
34 SceneKit 0x00000001aa432b1c 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1874716
35 libdispatch.dylib 0x0000000103356bd8 _dispatch_client_callout + 16
36 libdispatch.dylib 0x0000000103365858 _dispatch_lane_barrier_sync_invoke_and_complete + 124
37 SceneKit 0x00000001aa432aac 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 1874604
38 GPUToolsCore 0x000000010357d5f8 -[DYDisplayLinkInterposer forwardDisplayLinkCallback:] + 168
39 QuartzCore 0x000000019cc72860 8705A7FE-6FD6-301F-BE30-D32F13CB6C0E + 71776
40 IOKit 0x00000001972a4930 IODispatchCalloutFromCFMessage + 488
41 CoreFoundation 0x00000001962ba670 FF63481D-FB6A-353B-B2C1-AC4EAECF594D + 525936
42 CoreFoundation 0x00000001962e3e40 FF63481D-FB6A-353B-B2C1-AC4EAECF594D + 695872
43 CoreFoundation 0x00000001962e356c FF63481D-FB6A-353B-B2C1-AC4EAECF594D + 693612
44 CoreFoundation 0x00000001962de440 FF63481D-FB6A-353B-B2C1-AC4EAECF594D + 672832
45 CoreFoundation 0x00000001962dd8a0 CFRunLoopRunSpecific + 464
46 Foundation 0x000000019661d824 3C682044-3249-3938-8111-F2F12D066D2B + 30756
47 SceneKit 0x00000001aa32dcac 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 806060
48 SceneKit 0x00000001aa32def4 5DCB330E-65B4-3BA3-8648-D7BB0F7849CC + 806644
49 libsystem_pthread.dylib 0x00000001960821d0 _pthread_start + 124
50 libsystem_pthread.dylib 0x0000000196085ae0 thread_start + 8
)
我根据 Playing GIF using GIFU library in SceneKit causes app UI freeze any solution? UIView Animated being called from a background thread 使用 view.layer
而不是 view
解决了类似的问题:
material.diffuse.contents = self.myVC.view.layer
我联系了 Apple,他们说目前不支持此功能。他们根据保密协议向我发送了信息,因此我无法详细说明。
他们确实说过,如果这是我想要的功能,我可以向他们的团队发送请求,看看他们是否会考虑添加它。
当我将 SwiftUI view
包装到 HostingViewController
并将其传递给 SCNNode.material.diffuse.contents
时,我遇到了非常相似的问题。问题是,在 iOS15 之后,只显示视图的第一个 快照 - 没有动画,没有交互。
我尝试了很多解决方法,例如每秒将 view
渲染到 Image
30 次并将其传递给 SCNNode
,但没有任何效果。
我最终计算了透视变换矩阵并在 view
上应用了 CATransform3D
。
现在很有魅力。