2015-07-21 63 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,並且循環將不會執行。