Swift - 无法检索 CMSensorDataList 记录

Swift - Unable to retrieve CMSensorDataList records

我正在制作一个可以记录用户加速度的 Watch 应用程序。我使用 CoreMotion 框架中的 CMSensorRecorder 来执行此操作。

现在程序的流程是用户按下手表上的一个按钮,触发记录加速度30秒。在此之后,有6分钟的延迟(参考这里回答:,读取数据需要延迟),将加速度和时间戳数据打印到控制台。

现在我得到 "response invalid" 和 "Error occurred" 当 运行 应用程序。我在 info.plist 文件中添加了动作使用说明。

我对 Swift 和应用程序开发还很陌生,我担心我尝试访问数据的方式有问题。我在下面附上了控制台日志和代码。

任何人都可以提供一些消息的见解以及如何解决这个问题吗?我四处搜索但之前没有发现任何此问题的案例。干杯。

    func recordAcceleration(){
        if CMSensorRecorder.isAccelerometerRecordingAvailable(){
            print("recorder started")
            recorder.recordAccelerometer(forDuration: 30)   //forDuration controls how many seconds data is recorded for.
            print("recording done")
        }
    }

    func getData(){
        if let list = recorder.accelerometerData(from: Date(timeIntervalSinceNow: -400), to: Date()){
            print("listing data")

            for data in list{
                if let accData = data as? CMRecordedAccelerometerData{
                    let accX = accData.acceleration.x
                    let timestamp = accData.startDate
                    //Do something here.
                    print(accX)
                    print(timestamp)
                }
            }
        }

    }


    //Send data to iphone after time period.
    func sendData(dataBlock:CMSensorDataList){
        WCSession.default.transferUserInfo(["Data" : dataBlock])
    }



    //UI Elements
    @IBAction func recordButtonPressed() {
        print("button pressed")
        recordAcceleration()

        //A delay is needed to read the data properly.
        print("delaying 6 mins")
        perform(#selector(callback), with: nil, afterDelay: 6*60)

    }

    @objc func callback(){
        getData()
    }

    extension CMSensorDataList: Sequence {
    public func makeIterator() -> NSFastEnumerationIterator {
        return NSFastEnumerationIterator(self)
    }

Console output:

button pressed

recorder started

2019-03-12 12:12:12.568962+1100 app_name WatchKit Extension[233:5614] [Motion] Warning - invoking recordDataType:forDuration: on main may lead to deadlock.

2019-03-12 12:12:13.102712+1100 app_name WatchKit Extension[233:5614] [SensorRecorder] Response invalid.

recording done

delaying 6 mins

2019-03-12 12:18:13.115955+1100 app_name WatchKit Extension[233:5614] [Motion] Warning - invoking sensorDataFromDate:toDate:forType: on main may lead to deadlock.

2019-03-12 12:18:13.162476+1100 app_name WatchKit Extension[233:5753] [SensorRecorder] Error occurred while trying to retrieve accelerometer records!

我 运行 你的代码并没有得到 "Response invalid" 或 "Error occurred"。我确实收到了主线程警告。所以我换了一个后台线程,它工作正常。

此外,我认为您不需要等待六分钟。我改成了一分钟。

希望对您有所帮助。

let recorder = CMSensorRecorder()
@IBAction func recordAcceleration() {
    if CMSensorRecorder.isAccelerometerRecordingAvailable() {
        print("recorder started")
        DispatchQueue.global(qos: .background).async {
            self.recorder.recordAccelerometer(forDuration: 30)
        }
        perform(#selector(callback), with: nil, afterDelay: 1 * 60)
    }
}
@objc func callback(){
    DispatchQueue.global(qos: .background).async { self.getData() }
}
func getData(){
    print("getData started")
    if let list = recorder.accelerometerData(from: Date(timeIntervalSinceNow: -60), to: Date()) {
        print("listing data")
        for data in list{
            if let accData = data as? CMRecordedAccelerometerData{
                let accX = accData.acceleration.x
                let timestamp = accData.startDate
                //Do something here.
                print(accX)
                print(timestamp)
            }
        }
    }
}