2017-04-18 34 views
0

我用這個函數與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) 
} 

的問題是,如果我使用RunKeeper隔夜收集數據同步後獲得衛生工具包消耗的卡路里,我不能走散每天的價值。例如:

enter image description here

4月17日:3.1 4月18日:4.2

但我的應用程序裏面,我只得到以下數據: 4月17日:7.3

4月17日在衛生應用:

enter image description here

什麼想法?

回答