2015-07-21 64 views
0

我有一个函数,它获取设备记录的步骤总数,将其保存到一个变量,然后从每一天获取步骤数据,将它们添加到另一个变量,直到两个具有相同的价值。我需要这个以便应用程序知道何时停止将所有时间步数据保存到数组。步计数函数跳过线Swift

但是,这个函数的后半部分没有执行,我不知道为什么。下面是函数:

// allTimeStepTotal and allTimeStepSum are doubles that are defined with a value of 0.0 
func stepsAllTime(completion: (Double, NSError?) ->()) { 

    // The type of data we are requesting 
    let type = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount) 

    // Our search predicate which will fetch data from now until a day ago 
    let predicate = HKQuery.predicateForSamplesWithStartDate(NSDate.distantPast() as! NSDate, endDate: NSDate(), options: .None) 

    // The actual HealthKit Query which will fetch all of the steps and sub them up for us. 
    let query = HKSampleQuery(sampleType: type, predicate: predicate, limit: 0, sortDescriptors: nil) { query, results, error in 
     var steps: Double = 0 
     if results?.count > 0 { 
      for result in results as! [HKQuantitySample] { 
       steps += result.quantity.doubleValueForUnit(HKUnit.countUnit()) 
      } 
     } 
     completion(steps, error) 
     self.allTimeStepsTotal += steps 
     println("Total:") 
     println(self.allTimeStepsTotal) 
     println("Sum:") 
     println(self.allTimeStepsSum) 
    } 

    self.healthKitStore.executeQuery(query) 

    println("Moving On") 
    var x = 1 

    while self.allTimeStepsTotal > self.allTimeStepsSum { 
     x += -1 
     // The type of data we are requesting 
     let sampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount) 
     var daysAgo = -1 * x 
     var daysSince = (-1 * x) + 1 

     // Our search predicate which will fetch data from now until a day ago 
     let samplePredicate = HKQuery.predicateForSamplesWithStartDate(NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: daysAgo, toDate: NSDate(), options: nil), endDate: NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: daysSince, toDate: NSDate(), options: nil), options: .None) 

     // The actual HealthKit Query which will fetch all of the steps and sub them up for us. 
     let stepQuery = HKSampleQuery(sampleType: sampleType, predicate: samplePredicate, limit: 0, sortDescriptors: nil) { query, results, error in 
     var steps: Double = 0 

     if results?.count > 0 { 
      for result in results as! [HKQuantitySample] { 
       steps += result.quantity.doubleValueForUnit(HKUnit.countUnit()) 
      } 
     } 
     completion(steps, error) 
     self.allTimeStepsSum += steps 
     println("New Sum:") 
     println(self.allTimeStepsSum) 
    } 

    self.healthKitStore.executeQuery(stepQuery)  
} 

这里是电话:

healthManager.stepsAllTime({Double, NSError in 
     println("All Done") 
    }) 
    println("Finished executing stepsAllTime") 

谁能告诉我什么,我需要修复,或者什么地方出了错?

+0

这就是说,你有任何想法如何解决它?这个问题让我完全陷入困境。 – ButtonMasterBot

回答

1

假设allTimeStepsTotalallTimeStepsSum被初始化为0.0,这种职能,因为HKSampleQuery您创建不会执行的下半年异步执行,也就是说,它的工作之后调用resultHandler在一些在未来的时间while循环在你函数的后半部分被评估。条件self.allTimeStepsTotal > self.allTimeStepsSum将评估为false,因为这两个值仍然是0.0,并且循环将不会执行。