计步函数跳行 Swift

Step Count Function skipping lines 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")

谁能告诉我我需要修复什么,或者出了什么问题?

假设 allTimeStepsTotalallTimeStepsSum 初始化为 0.0,该函数的后半部分将不会执行,因为您创建的 HKSampleQuery 是异步执行的——也就是说,它会在函数后半部分的 while 循环被求值后的某个时间调用 resultHandler 。条件 self.allTimeStepsTotal > self.allTimeStepsSum 的计算结果为假,因为两个值仍然是 0.0,循环将不会执行。