如何从查询中获取信息

时间:2015-08-17 16:35:48

标签: ios swift swift2 xcode7 health-kit

我创建了一个全局变量来存储信息

var stepsCount:Float = 0

并用它来存储查询中的步骤数

let stepsCountQuery = HKStatisticsQuery(quantityType: stepsCountType,quantitySamplePredicate: predicate, options: .CumulativeSum, completionHandler: {
(query, results, error) in
if results == nil{
    print(error)
}
else {
    let info = results!.sumQuantity()
    if info == nil {
        print("no information available")
    }
    else {
        stepsCount = Float(info!.doubleValueForUnit(HKUnit.countUnit()))
    }
}
})

但似乎在为查询辩解时变量没有改变 任何想法??

1 个答案:

答案 0 :(得分:0)

你的完成块看起来应该更像这样:

let stepsCountQuery = HKStatisticsQuery(quantityType: stepsCountType,quantitySamplePredicate: predicate, options: .CumulativeSum, completionHandler: { 
       (query, results, error) in
    if results == nil{
        print(error)
    }
    else {
         let info = results!.sumQuantity()
         if info == nil {
             print("no information available")
         }
         else {
             stepsCount = Float(info!.doubleValueForUnit(HKUnit.countUnit()))
             dispatch_async(dispatch_get_main_queue(), {
                   stepsTracker.setProgress(stepsCount/1300 , animated: true) 
              })
         }
    }
 })