CMPedometer数据超过7天

时间:2017-07-06 16:08:24

标签: ios swift core-motion

我正在尝试从CMPedometer收集步骤10天。它工作正常7天,但最近三天返回0。

以下是代码:

var days:[String] = []
var stepsTaken:[Int] = []
let activityManager = CMMotionActivityManager()
let pedoMeter = CMPedometer()

var cnt = 0

override func viewDidLoad() {
    super.viewDidLoad()
    getDataForLast10Days()
}

func getDataForLast10Days() {
    if(CMPedometer.isStepCountingAvailable()){
        let serialQueue : DispatchQueue  = DispatchQueue(label: "com.example.MyQueue", attributes: .concurrent)
        let formatter = DateFormatter()
        formatter.dateFormat = "d MMM"
        serialQueue.sync(execute: { () -> Void in
            let today = NSDate()
            for day in 1...10{
                let from = NSDate(timeIntervalSinceNow: Double(-7+day) ))
                let hour = Calendar.current.component(.hour, from: from as Date)
                let min = Calendar.current.component(.minute, from: from as Date)
                let sec = Calendar.current.component(.second, from: from as Date)
                let timeToSub = (hour * 60 + min) * 60 + sec
                let fromDate = NSDate(timeIntervalSinceNow: (Double(-10+day) * (86400 ) ) - Double(timeToSub))
                let toDate = NSDate(timeIntervalSinceNow: (Double(-10+day+1) * (86400) ) - Double(timeToSub))
                let dtStr = formatter.string(from: (toDate as Date))

                self.pedoMeter.queryPedometerData(from: fromDate as Date , to: toDate as Date) { (data : CMPedometerData!, error) -> Void in
                    //print("From Date: \(fromDate)","\n\n")
                    if(error == nil){
                        // print(data,"\n\n")
                        print("\(dtStr) : \(data.numberOfSteps)")
                        self.days.append(dtStr)
                        self.stepsTaken.append(Int(data.numberOfSteps))
                    }
                }
            }
        })
    }
}

但是随着我创建应用程序以来日子的增加,数据每天都会增加一天。我创建应用程序已经过去2天了。它有9天的数据,依此类推。我该如何解决这个问题?

请让我知道出了什么问题。

1 个答案:

答案 0 :(得分:1)

一切都很好。

根据实例方法queryPedometerData(from:to:withHandler:)

的方法定义
  

只存储过去七天的数据并可供您使用   检索。指定超过七天的开始日期   过去只返回可用的数据。

See Apple's Documentation here.