2017-10-22 1 views
0

나는 단계마다 시간 계산 얻으려고이를 위해 내가 할 :HealthKit이 잘못된 시간을 반환하는 이유는 무엇입니까?

func retrieveSteps(completion: @escaping (_ stepsCount: Double) -> Void) { 
    let stepsCount = HKQuantityType.quantityType(forIdentifier: .stepCount) 

    let date = Date() 
    let calendar = Calendar(identifier: .gregorian) 
    let newDate = calendar.startOfDay(for: date) 

    let predicate = HKQuery.predicateForSamples(withStart: newDate, end: date, options: [.strictStartDate]) 
    var interval = DateComponents() 
    interval.hour = 1 

    let query = HKStatisticsCollectionQuery(quantityType: stepsCount!, quantitySamplePredicate: predicate, options: [.cumulativeSum], anchorDate: newDate, intervalComponents: interval) 

    query.initialResultsHandler = { query, result, error in 
     if let stats = result { 
      stats.enumerateStatistics(from: newDate, to: date) { statistics, _ in 
       if let quantity = statistics.sumQuantity() { 

        let steps = quantity.doubleValue(for: HKUnit.count()) 
        print("Steps: \(steps) for: \(statistics.endDate)") 

        completion(steps) 
       } 
      } 
     } 
    } 

    HKHealthStore().execute(query) 
} 

을 그리고 그것을 실행할 때 잘못된 날짜 값을 얻는다. 예 :

Steps: 28.3782023430627 for: 2017-10-22 10:00:00 +0000 

그러나 건강 앱에서는 시간이 11:58입니다. 왜 제가 10:00을 얻고 있습니까? 어떻게 개선 할 수 있습니까?

+0

시, 분 및 초 형식을 지정하고 출력하십시오. – Lumialxk

+0

@ Lumialxk하지만 어떻게 도움이 될까요? '10 : 00'과'11 : 58' 사이 - 2 hours =/ –

+0

10 시가 시간대 일 뿐이라는 것이 궁금합니다. – Lumialxk

답변

0

HKStatisticsCollectionQuery에서 가져온 통계 오브젝트는 해당 시간 범위 내에있는 특정 샘플의 날짜가 아니라 제공된 간격 컴포 M 트의 시간 범위를 나타냄니다. HealthKit에서 가장 최근 단계 수 샘플의 날짜를 찾으려면 HKSampleQuery을 사용해야합니다.

관련 문제