title: Step Count Function skipping lines Swift
body: <p>I have a function that takes the total number of steps recorded by the device, saves it to a variable, and then gets the step data from each day, adding them to another variable until the two have the same value. I need this in order to for the app to know when to stop when it is saving the all time step data to an array.</p>
  
  <p>However, the second half of this function does not execute, and I have no idea why. Here is the function:</p>
  
  <pre><code>// allTimeStepTotal and allTimeStepSum are doubles that are defined with a value of 0.0
  func stepsAllTime(completion: (Double, NSError?) -&gt; () ) {
  
      // 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 &gt; 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 &gt; 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 &gt; 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)       
  }
  </code></pre>
  
  <p>And here is the call:</p>
  
  <pre><code>    healthManager.stepsAllTime({Double, NSError in
          println("All Done")
      })
      println("Finished executing stepsAllTime")
  </code></pre>
  
  <p>Can anyone tell me what I need to fix, or what went wrong?</p>