2017-04-18 2 views
0

私は、問題は、私は一晩のデータを収集するためにRunKeeperを使用している場合、私は分離取得することはできませんですRunKeeper健康キットから一晩やけどのカロリー

func getActiveEnergy(currentDate: Date ,completion: @escaping ((_ totalEnergy: Double) -> Void)) { 
    let calendar = Calendar.current 
    var totalBurnedEnergy = Double() 
    let startOfDay = Int((currentDate.timeIntervalSince1970/86400)+1)*86400 
    let startOfDayDate = Date(timeIntervalSince1970: Double(startOfDay)) 
    // Get the start of the day 
    let newDate = calendar.startOfDay(for: startOfDayDate) 
    let startDate: Date = calendar.date(byAdding: Calendar.Component.day, value: -1, to: newDate)! 

    // Set the Predicates 
    let predicate = HKQuery.predicateForSamples(withStart: startDate as Date, end: newDate as Date, options: .strictStartDate) 

    // Perform the Query 
    let energySampleType = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned) 

    let query = HKSampleQuery(sampleType: energySampleType!, predicate: predicate, limit: 0, sortDescriptors: nil, resultsHandler: { 
     (query, results, error) in 
     if results == nil { 
      print("There was an error running the query: \(error)") 
     } 

     DispatchQueue.main.async { 

      for activity in results as! [HKQuantitySample] 
      { 
       let calories = activity.quantity.doubleValue(for: HKUnit.kilocalorie()) 
       totalBurnedEnergy = totalBurnedEnergy + calories 
      } 
      completion(totalBurnedEnergy) 
     } 
    }) 
    self.healthStore.execute(query) 
} 

と同期した後、健康キットから消費カロリーを取得するには、この機能を使用します毎日の価値。例:

enter image description here

4月17日:3.1 4月18日: 4月17日:7.3

4月17日の健康アプリで

しかし、私のアプリの内部4.2は、私は下のデータを取得します:

enter image description here

任意のアイデア?

答えて

関連する問題