Swift-无法检索CMSensorDataList记录

时间:2019-03-12 01:50:17

标签: swift core-motion watch-os-5

我正在制作一个可以记录用户加速度的Watch应用。我使用了CMSensorRecorder框架中的CoreMotion来做到这一点。

当前程序的流程是用户按下手表上的按钮,这会触发加速度记录30秒。之后,会有6分钟的延迟(请在此处参考答案:watchOS2 - CMSensorRecorder,读取数据需要延迟),并且加速度和时间戳数据会打印到控制台。

现在,运行该应用程序时出现“响应无效”和“发生错误”。我已经在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!

1 个答案:

答案 0 :(得分:0)

我运行了您的代码,但未收到“响应无效”或“发生错误”。我确实收到了主线程警告。所以我换了一个后台线程,它工作正常。

此外,我认为您无需等待六分钟。我将其更改为一分钟。

我希望这会有所帮助。

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)
            }
        }
    }
}