CMotionManager在实例化时崩溃

时间:2018-10-24 23:27:02

标签: ios swift core-motion ios12

为Core Motion实例化CMMotionManager对象时出现崩溃。这是在运行iOS 12.0.1的iPhone X上。

我可以使用以下视图控制器,通过单视图应用程序可靠地重现此内容。

import UIKit
import CoreMotion

class ViewController: UIViewController {
    var motion: CMMotionManager?
    override func viewDidLoad() {
        super.viewDidLoad()

        // This causes a crash on iPhone Xs, iOS 12.0.1
        self.motion = CMMotionManager()
    }
}

完整的示例项目位于https://github.com/doctorcolinsmith/motiontestcrash/tree/master

运行上述命令时,调试器中出现以下输出,导致线程出错。

=================================================================
Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 3634, TID: 630341, Thread name: com.apple.CoreMotion.MotionThread, Queue name: com.apple.root.default-qos.overcommit, QoS: 0
Backtrace:
4   libobjc.A.dylib                     0x000000019b0d3894 <redacted> + 56
5   CoreMotion                          0x00000001a19337a4 CoreMotion + 305060
6   CoreMotion                          0x00000001a1933cd8 CoreMotion + 306392
7   CoreMotion                          0x00000001a1933be8 CoreMotion + 306152
8   CoreMotion                          0x00000001a19653cc CoreMotion + 508876
9   CoreMotion                          0x00000001a196542c CoreMotion + 508972
10  CoreFoundation                      0x000000019be6c888 <redacted> + 28
11  CoreFoundation                      0x000000019be6c16c <redacted> + 276
12  CoreFoundation                      0x000000019be67470 <redacted> + 2324
13  CoreFoundation                      0x000000019be66844 CFRunLoopRunSpecific + 452
14  CoreFoundation                      0x000000019be675a8 CFRunLoopRun + 84
15  CoreMotion                          0x00000001a1964d64 CoreMotion + 507236
16  libsystem_pthread.dylib             0x000000019bae1a04 <redacted> + 132
17  libsystem_pthread.dylib             0x000000019bae1960 _pthread_start + 52
18  libsystem_pthread.dylib             0x000000019bae9df4 thread_start + 4
2018-10-24 16:19:31.423680-0700 motiontest[3634:630341] [reports] Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 3634, TID: 630341, Thread name: com.apple.CoreMotion.MotionThread, Queue name: com.apple.root.default-qos.overcommit, QoS: 0
Backtrace:
4   libobjc.A.dylib                     0x000000019b0d3894 <redacted> + 56
5   CoreMotion                          0x00000001a19337a4 CoreMotion + 305060
6   CoreMotion                          0x00000001a1933cd8 CoreMotion + 306392
7   CoreMotion                          0x00000001a1933be8 CoreMotion + 306152
8   CoreMotion                          0x00000001a19653cc CoreMotion + 508876
9   CoreMotion                          0x00000001a196542c CoreMotion + 508972
10  CoreFoundation                      0x000000019be6c888 <redacted> + 28
11  CoreFoundation                      0x000000019be6c16c <redacted> + 276
12  CoreFoundation                      0x000000019be67470 <redacted> + 2324
13  CoreFoundation                      0x000000019be66844 CFRunLoopRunSpecific + 452
14  CoreFoundation                      0x000000019be675a8 CFRunLoopRun + 84
15  CoreMotion                          0x00000001a1964d64 CoreMotion + 507236
16  libsystem_pthread.dylib             0x000000019bae1a04 <redacted> + 132
17  libsystem_pthread.dylib             0x000000019bae1960 _pthread_start + 52
18  libsystem_pthread.dylib             0x000000019bae9df4 thread_start + 4
(lldb) 

有人以前遇到过这种情况吗,或者对如何解决崩溃有想法?

2 个答案:

答案 0 :(得分:4)

好吧,我已经在物理上测试了您的应用程序 iPhone 6S iPhone 7 -显然是在iOS 12.0.1上进行的。我还检查了项目中的几件事,根本无法重现您的问题-甚至在模拟器中也是如此-而且我认为无法下载具有特定修复程序的模拟器( 12.0.x ),仅具有次要版本更新-因此, 12.1 是下一个更新。这看起来确实是在iPhone XS硬件上特定的,我建议您用Apple support来解决问题。

编辑:好的,我看到您已经填写了有关 openradar 的错误报告。 This guy甚至在 openradar-mirror 上引用了错误报告状态。

答案 1 :(得分:-1)

我对CoreMotion不太了解,但是错误消息告诉您您正在尝试在后台线程上调用UI操作。尝试将代码放入:

DispatchQueue.main.async {
    //your code here
}

似乎您也在尝试在viewDidLoad()中执行与UI相关的操作。从技术上讲,这很好,但是请记住,此时某些视图可能尚未布置。