gpt4 book ai didi

iOS 12.2 : CMMotionManager is blocking the main thread

转载 作者:行者123 更新时间:2023-11-29 05:51:29 25 4
gpt4 key购买 nike

我对 CoreMotion 和以下代码有疑问:

让motionManager = CMMotionManager()

它阻塞了我的主线程 4-5 秒,我不知道为什么。当我将 iPhone XR 更新到 12.2 时,出现了该问题。在 12.1.3 上,它不会阻塞 iPhone 6S 的主线程。

我认为可能是硬件问题或iOS版本问题。

谢谢

最佳答案

CoreMotion 在初始化期间自己做了很多事情。
将初始化移至不同的线程。
编辑:

我可以在开发版 iPhone Xs 上确认 12.2 的问题。在真实使用的设备上没有问题。我还看到违规警告告诉 CoreMotion 尝试从后台线程访问 Applicationstate。

但是,将 init 移动到单独的线程可以修复此处挂起的所有 UI。 coremotion 的初始化仍然需要一段时间。我创建了一个空的单 View 应用程序项目并更改了 ViewController 类

class ViewController: UIViewController {

var motionManager: CMMotionManager?

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
view.backgroundColor = UIColor.red
DispatchQueue.global().async {
self.motionManager = CMMotionManager()
DispatchQueue.main.async {
self.view.backgroundColor = UIColor.green
}
}
view.backgroundColor = UIColor.yellow
}

}

如果没有单独的线程,红色就会保留。使用单独的线程,颜色在 dev XS 上立即变为黄色,最后变为绿色,在我的 iPhone 8Plus 上立即变为绿色。

添加:
有趣的是,在没有附加 XCode 的情况下,开发设备没有任何问题。尝试在不连接到调试器的情况下运行代码。

关于iOS 12.2 : CMMotionManager is blocking the main thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55629406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com