2017-10-22 16 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時間=/ –

+0

あなたのタイムゾーンは10:00と思っています。 – Lumialxk

答えて

0

HKStatisticsCollectionQueryから返される統計オブジェクトは、指定した間隔の範囲内にある特定のサンプルの日付ではなく、指定した間隔コンポーネントのサイズの範囲を表します。 HealthKitで最新のステップカウントサンプルの日付を検索する場合は、HKSampleQueryを使用する必要があります。

関連する問題