我有一个函数,可以记录设备记录的总步数,将其保存到变量中,然后获取每天的步数数据,并将它们添加到另一个变量中,直到两者具有相同的值为止。我需要这样做,以便应用程序知道将所有时间步长数据保存到数组时何时停止。

但是,此功能的后半部分不执行,我也不知道为什么。这是函数:

// 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是异步执行的,也就是说,它有时会调用resultHandler以后在函数后半部分的while循环之后进行评估。由于两个值仍为self.allTimeStepsTotal > self.allTimeStepsSum,条件0.0的计算结果将为false,并且循环将不执行。

09-06 01:40