CMPedometer queryPedometerData() returning 0 steps when steps exist

╄→尐↘猪︶ㄣ 提交于 2019-12-11 19:48:20

问题


There seems to be a bug in the CMPedometer queryPedometerData() method. The method is returning 0 steps for certain end times, but the same query with the timestamp 1 second higher or lower returns the correct number of steps

e.g.

self.getStepsBetweenDates(NSDate(timeIntervalSince1970: 1543392126) as Date, date2:  NSDate(timeIntervalSince1970: 1543393044) as Date) returns (Int) 1488

self.getStepsBetweenDates(NSDate(timeIntervalSince1970: 1543392126) as Date, date2:  NSDate(timeIntervalSince1970: 1543393045) as Date) returns (Int) 0

self.getStepsBetweenDates(NSDate(timeIntervalSince1970: 1543392126) as Date, date2:  NSDate(timeIntervalSince1970: 1543393046) as Date) returns (Int) 1488

getStepsBetweenDates method looks like this

func getStepsBetweenDates(_ date1: Date, date2: Date) -> Int{

    let group = DispatchGroup()
    group.enter()

    var steps = 0

    self.pedometer.queryPedometerData(from: date1, to: date2, withHandler: {
      pData, error in
      if let e = error{
          print("Error querying pedometer", e.localizedDescription)
      }else{
        if let data = pData{
          steps = Int(data.numberOfSteps)
        }
        group.leave()
      }
    })

    _ = group.wait(timeout: DispatchTime.distantFuture)
    return steps

  }

回答1:


queryPedometerData is an asynchronous call

Ordinarily you would not want to return the steps in the getStepsBetweenDates call because it is asynchronous.

If you changed the var steps = 0 to var steps = [some random int] then it's likely you'll get that number back instead of 0 because of the race condition set up.

More ideally would be to implement your code as a closure/callback or another form of asynchronous handling.

eg:

self.pedometer.queryPedometerData(from: date1, to: date2) { (data, error) in
  // Do something here with data.numberOfSteps    
}

Here's an article on Medium about async code:

https://medium.com/ios-os-x-development/managing-async-code-in-swift-d7be44cae89f



来源:https://stackoverflow.com/questions/53517678/cmpedometer-querypedometerdata-returning-0-steps-when-steps-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!