世界跟踪性能受到资源限制的影响
World tracking performance is being affected by resource constraints
我做了一个应用程序,可以跟踪手的位置并根据手指的位置移动 AR 对象。主要方法使用视觉框架。代码运行没有任何问题,但是一旦在相机中检测到手,程序就会冻结将近一秒钟,然后才能正常运行。这是我调用视觉框架的代码。
// MARK: - SCNSceneRendererDelegate
func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
updateCoreML()
}
这是控制台日志中的输出。每当我重新启动应用程序时,它总是会打印出来。
=================================================================
Main Thread Checker: UI API called on a background thread: -[UIView bounds]
PID: 4771, TID: 5232327, Thread name: com.apple.scenekit.scnview-renderer, Queue name: com.apple.scenekit.renderingQueue.ARSCNView0x106705f90, QoS: 33
Backtrace:
4 AR_1 0x00000001029146b4 $s4AR_114ViewControllerC12updateCoreMLyyF + 11688
5 AR_1 0x0000000102917a0c $s4AR_114ViewControllerC8renderer_14didRenderScene6atTimeySo16SCNSceneRenderer_p_So0J0CSdtF + 72
6 AR_1 0x0000000102917a74 $s4AR_114ViewControllerC8renderer_14didRenderScene6atTimeySo16SCNSceneRenderer_p_So0J0CSdtFTo + 92
7 SceneKit 0x00000001e0d578f0 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1505520
8 SceneKit 0x00000001e0dde3d4 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 2057172
9 SceneKit 0x00000001e0c56040 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 450624
10 SceneKit 0x00000001e0c5785c EC930BC7-1D62-3C34-AE79-B49A41F6198E + 456796
11 SceneKit 0x00000001e0d5d010 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1527824
12 SceneKit 0x00000001e0d5d87c EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1529980
13 SceneKit 0x00000001e0d5de20 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1531424
14 SceneKit 0x00000001e0d5e204 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1532420
15 SceneKit 0x00000001e0e009a0 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 2197920
16 SceneKit 0x00000001e0cb5958 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 842072
17 SceneKit 0x00000001e0dc9be0 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1973216
18 libdispatch.dylib 0x00000001030a56c0 _dispatch_client_callout + 20
19 libdispatch.dylib 0x00000001030b5f14 _dispatch_lane_barrier_sync_invoke_and_complete + 176
20 SceneKit 0x00000001e0dc9b64 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1973092
21 GPUToolsCore 0x00000001032a4f44 -[DYDisplayLinkInterposer forwardDisplayLinkCallback:] + 172
22 QuartzCore 0x00000001afc8b6fc 8510F139-0824-3686-A9AA-3E198539A021 + 83708
23 QuartzCore 0x00000001afd64a80 8510F139-0824-3686-A9AA-3E198539A021 + 973440
24 CoreFoundation 0x00000001ac910dd0 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 511440
25 CoreFoundation 0x00000001ac935fe8 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 663528
26 CoreFoundation 0x00000001ac935378 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 660344
27 CoreFoundation 0x00000001ac92f08c 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 635020
28 CoreFoundation 0x00000001ac92e21c CFRunLoopRunSpecific + 600
29 Foundation 0x00000001adbdddf0 7698BF3E-0CF6-31C0-85E9-562714F01276 + 36336
30 SceneKit 0x00000001e0cb5ea4 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 843428
31 SceneKit 0x00000001e0cb61bc EC930BC7-1D62-3C34-AE79-B49A41F6198E + 844220
32 libsystem_pthread.dylib 0x00000001f8467cb0 _pthread_start + 320
33 libsystem_pthread.dylib 0x00000001f8470778 thread_start + 8
2021-03-04 20:42:49.062465+0900 AR_1[4771:5232327] [reports] Main Thread Checker: UI API called on a background thread: -[UIView bounds]
PID: 4771, TID: 5232327, Thread name: com.apple.scenekit.scnview-renderer, Queue name: com.apple.scenekit.renderingQueue.ARSCNView0x106705f90, QoS: 33
Backtrace:
4 AR_1 0x00000001029146b4 $s4AR_114ViewControllerC12updateCoreMLyyF + 11688
5 AR_1 0x0000000102917a0c $s4AR_114ViewControllerC8renderer_14didRenderScene6atTimeySo16SCNSceneRenderer_p_So0J0CSdtF + 72
6 AR_1 0x0000000102917a74 $s4AR_114ViewControllerC8renderer_14didRenderScene6atTimeySo16SCNSceneRenderer_p_So0J0CSdtFTo + 92
7 SceneKit 0x00000001e0d578f0 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1505520
8 SceneKit 0x00000001e0dde3d4 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 2057172
9 SceneKit 0x00000001e0c56040 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 450624
10 SceneKit 0x00000001e0c5785c EC930BC7-1D62-3C34-AE79-B49A41F6198E + 456796
11 SceneKit 0x00000001e0d5d010 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1527824
12 SceneKit 0x00000001e0d5d87c EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1529980
13 SceneKit 0x00000001e0d5de20 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1531424
14 SceneKit 0x00000001e0d5e204 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1532420
15 SceneKit 0x00000001e0e009a0 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 2197920
16 SceneKit 0x00000001e0cb5958 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 842072
17 SceneKit 0x00000001e0dc9be0 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1973216
18 libdispatch.dylib 0x00000001030a56c0 _dispatch_client_callout + 20
19 libdispatch.dylib 0x00000001030b5f14 _dispatch_lane_barrier_sync_invoke_and_complete + 176
20 SceneKit 0x00000001e0dc9b64 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1973092
21 GPUToolsCore 0x00000001032a4f44 -[DYDisplayLinkInterposer forwardDisplayLinkCallback:] + 172
22 QuartzCore 0x00000001afc8b6fc 8510F139-0824-3686-A9AA-3E198539A021 + 83708
23 QuartzCore 0x00000001afd64a80 8510F139-0824-3686-A9AA-3E198539A021 + 973440
24 CoreFoundation 0x00000001ac910dd0 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 511440
25 CoreFoundation 0x00000001ac935fe8 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 663528
26 CoreFoundation 0x00000001ac935378 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 660344
27 CoreFoundation 0x00000001ac92f08c 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 635020
28 CoreFoundation 0x00000001ac92e21c CFRunLoopRunSpecific + 600
29 Foundation 0x00000001adbdddf0 7698BF3E-0CF6-31C0-85E9-562714F01276 + 36336
30 SceneKit 0x00000001e0cb5ea4 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 843428
31 SceneKit 0x00000001e0cb61bc EC930BC7-1D62-3C34-AE79-B49A41F6198E + 844220
32 libsystem_pthread.dylib 0x00000001f8467cb0 _pthread_start + 320
33 libsystem_pthread.dylib 0x00000001f8470778 thread_start + 8
2021-03-04 20:42:57.812761+0900 AR_1[4771:5233782] [Technique] ARWorldTrackingTechnique <0x10341bc30>: World tracking performance is being affected by resource constraints [0]
我认为调用函数的时间不正确,这就是为什么它说性能受到资源限制的影响。我尝试使用 updateAtTime
委托,但它不起作用。调用单独的线程也可能有效,但我不确定。
根据日志,在 updateCoreML()
中,您正在调用与 UIKit 更改有关的内容。
你能尝试在下面的事情中包装这个函数吗?
// MARK: - SCNSceneRendererDelegate
func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
DispatchQueue.main.async { [weak self] in
self?.updateCoreML()
}
}
- 为什么主队列修复有效
您正在主队列之外调用 UIKit 函数(view.bounds
在您的情况下),这在 Swift.
中是不允许的
- 关于ARC和
[weak self]
的解释:
Weak
指块内对象的自动引用计数(ARC)。
引用计数 - Swift的内存管理。
你可以在这里读更多关于它的内容:
https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html
在这种情况下,我们在 闭包捕获列表 (参见:Closure Capture List)中使用 [weak self]
来确保自己(在您的情况下 ViewController)不是强烈保留。
换句话说,如果你的ViewController实例要被释放,在块执行被调用之前,实例将被释放,self将等于nil 一旦块被调用。
我做了一个应用程序,可以跟踪手的位置并根据手指的位置移动 AR 对象。主要方法使用视觉框架。代码运行没有任何问题,但是一旦在相机中检测到手,程序就会冻结将近一秒钟,然后才能正常运行。这是我调用视觉框架的代码。
// MARK: - SCNSceneRendererDelegate
func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
updateCoreML()
}
这是控制台日志中的输出。每当我重新启动应用程序时,它总是会打印出来。
=================================================================
Main Thread Checker: UI API called on a background thread: -[UIView bounds]
PID: 4771, TID: 5232327, Thread name: com.apple.scenekit.scnview-renderer, Queue name: com.apple.scenekit.renderingQueue.ARSCNView0x106705f90, QoS: 33
Backtrace:
4 AR_1 0x00000001029146b4 $s4AR_114ViewControllerC12updateCoreMLyyF + 11688
5 AR_1 0x0000000102917a0c $s4AR_114ViewControllerC8renderer_14didRenderScene6atTimeySo16SCNSceneRenderer_p_So0J0CSdtF + 72
6 AR_1 0x0000000102917a74 $s4AR_114ViewControllerC8renderer_14didRenderScene6atTimeySo16SCNSceneRenderer_p_So0J0CSdtFTo + 92
7 SceneKit 0x00000001e0d578f0 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1505520
8 SceneKit 0x00000001e0dde3d4 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 2057172
9 SceneKit 0x00000001e0c56040 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 450624
10 SceneKit 0x00000001e0c5785c EC930BC7-1D62-3C34-AE79-B49A41F6198E + 456796
11 SceneKit 0x00000001e0d5d010 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1527824
12 SceneKit 0x00000001e0d5d87c EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1529980
13 SceneKit 0x00000001e0d5de20 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1531424
14 SceneKit 0x00000001e0d5e204 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1532420
15 SceneKit 0x00000001e0e009a0 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 2197920
16 SceneKit 0x00000001e0cb5958 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 842072
17 SceneKit 0x00000001e0dc9be0 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1973216
18 libdispatch.dylib 0x00000001030a56c0 _dispatch_client_callout + 20
19 libdispatch.dylib 0x00000001030b5f14 _dispatch_lane_barrier_sync_invoke_and_complete + 176
20 SceneKit 0x00000001e0dc9b64 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1973092
21 GPUToolsCore 0x00000001032a4f44 -[DYDisplayLinkInterposer forwardDisplayLinkCallback:] + 172
22 QuartzCore 0x00000001afc8b6fc 8510F139-0824-3686-A9AA-3E198539A021 + 83708
23 QuartzCore 0x00000001afd64a80 8510F139-0824-3686-A9AA-3E198539A021 + 973440
24 CoreFoundation 0x00000001ac910dd0 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 511440
25 CoreFoundation 0x00000001ac935fe8 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 663528
26 CoreFoundation 0x00000001ac935378 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 660344
27 CoreFoundation 0x00000001ac92f08c 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 635020
28 CoreFoundation 0x00000001ac92e21c CFRunLoopRunSpecific + 600
29 Foundation 0x00000001adbdddf0 7698BF3E-0CF6-31C0-85E9-562714F01276 + 36336
30 SceneKit 0x00000001e0cb5ea4 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 843428
31 SceneKit 0x00000001e0cb61bc EC930BC7-1D62-3C34-AE79-B49A41F6198E + 844220
32 libsystem_pthread.dylib 0x00000001f8467cb0 _pthread_start + 320
33 libsystem_pthread.dylib 0x00000001f8470778 thread_start + 8
2021-03-04 20:42:49.062465+0900 AR_1[4771:5232327] [reports] Main Thread Checker: UI API called on a background thread: -[UIView bounds]
PID: 4771, TID: 5232327, Thread name: com.apple.scenekit.scnview-renderer, Queue name: com.apple.scenekit.renderingQueue.ARSCNView0x106705f90, QoS: 33
Backtrace:
4 AR_1 0x00000001029146b4 $s4AR_114ViewControllerC12updateCoreMLyyF + 11688
5 AR_1 0x0000000102917a0c $s4AR_114ViewControllerC8renderer_14didRenderScene6atTimeySo16SCNSceneRenderer_p_So0J0CSdtF + 72
6 AR_1 0x0000000102917a74 $s4AR_114ViewControllerC8renderer_14didRenderScene6atTimeySo16SCNSceneRenderer_p_So0J0CSdtFTo + 92
7 SceneKit 0x00000001e0d578f0 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1505520
8 SceneKit 0x00000001e0dde3d4 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 2057172
9 SceneKit 0x00000001e0c56040 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 450624
10 SceneKit 0x00000001e0c5785c EC930BC7-1D62-3C34-AE79-B49A41F6198E + 456796
11 SceneKit 0x00000001e0d5d010 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1527824
12 SceneKit 0x00000001e0d5d87c EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1529980
13 SceneKit 0x00000001e0d5de20 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1531424
14 SceneKit 0x00000001e0d5e204 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1532420
15 SceneKit 0x00000001e0e009a0 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 2197920
16 SceneKit 0x00000001e0cb5958 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 842072
17 SceneKit 0x00000001e0dc9be0 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1973216
18 libdispatch.dylib 0x00000001030a56c0 _dispatch_client_callout + 20
19 libdispatch.dylib 0x00000001030b5f14 _dispatch_lane_barrier_sync_invoke_and_complete + 176
20 SceneKit 0x00000001e0dc9b64 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 1973092
21 GPUToolsCore 0x00000001032a4f44 -[DYDisplayLinkInterposer forwardDisplayLinkCallback:] + 172
22 QuartzCore 0x00000001afc8b6fc 8510F139-0824-3686-A9AA-3E198539A021 + 83708
23 QuartzCore 0x00000001afd64a80 8510F139-0824-3686-A9AA-3E198539A021 + 973440
24 CoreFoundation 0x00000001ac910dd0 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 511440
25 CoreFoundation 0x00000001ac935fe8 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 663528
26 CoreFoundation 0x00000001ac935378 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 660344
27 CoreFoundation 0x00000001ac92f08c 727F2644-EB4E-3D57-BC2E-E6803BA92366 + 635020
28 CoreFoundation 0x00000001ac92e21c CFRunLoopRunSpecific + 600
29 Foundation 0x00000001adbdddf0 7698BF3E-0CF6-31C0-85E9-562714F01276 + 36336
30 SceneKit 0x00000001e0cb5ea4 EC930BC7-1D62-3C34-AE79-B49A41F6198E + 843428
31 SceneKit 0x00000001e0cb61bc EC930BC7-1D62-3C34-AE79-B49A41F6198E + 844220
32 libsystem_pthread.dylib 0x00000001f8467cb0 _pthread_start + 320
33 libsystem_pthread.dylib 0x00000001f8470778 thread_start + 8
2021-03-04 20:42:57.812761+0900 AR_1[4771:5233782] [Technique] ARWorldTrackingTechnique <0x10341bc30>: World tracking performance is being affected by resource constraints [0]
我认为调用函数的时间不正确,这就是为什么它说性能受到资源限制的影响。我尝试使用 updateAtTime
委托,但它不起作用。调用单独的线程也可能有效,但我不确定。
根据日志,在 updateCoreML()
中,您正在调用与 UIKit 更改有关的内容。
你能尝试在下面的事情中包装这个函数吗?
// MARK: - SCNSceneRendererDelegate
func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
DispatchQueue.main.async { [weak self] in
self?.updateCoreML()
}
}
- 为什么主队列修复有效
您正在主队列之外调用 UIKit 函数(view.bounds
在您的情况下),这在 Swift.
- 关于ARC和
[weak self]
的解释:
Weak
指块内对象的自动引用计数(ARC)。
引用计数 - Swift的内存管理。 你可以在这里读更多关于它的内容: https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html
在这种情况下,我们在 闭包捕获列表 (参见:Closure Capture List)中使用 [weak self]
来确保自己(在您的情况下 ViewController)不是强烈保留。
换句话说,如果你的ViewController实例要被释放,在块执行被调用之前,实例将被释放,self将等于nil 一旦块被调用。