Step Count函数跳过线Swift

时间:2015-07-21 17:39:21

标签: ios swift health-kit

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

但是,这个函数的后半部分没有执行,我不明白为什么。这是功能:

// 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")

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

1 个答案:

答案 0 :(得分:1)

假设allTimeStepsTotalallTimeStepsSum被初始化为0.0,该功能的后半部分将无法执行,因为您已创建HKSampleQuery异步执行 - 也就是说,在评估函数后半部分的resultHandler循环后,它会在将来的某个时间调用while。条件self.allTimeStepsTotal > self.allTimeStepsSum将评估为false,因为两个值仍为0.0,并且循环将不会执行。