Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何等待异步函数Swift_Ios_Swift - Fatal编程技术网

Ios 如何等待异步函数Swift

Ios 如何等待异步函数Swift,ios,swift,Ios,Swift,我有一个收集步骤数据的函数,但我需要一种方法使它在循环之前等待查询完成。我意识到关于这个问题还有其他问题,但我不知道如何解决它。功能如下: func stepsAllTime(completion: (Double, NSError?) -> () ) { var stopStart = true while stopStart { x += -1 // The type of data we are requesting

我有一个收集步骤数据的函数,但我需要一种方法使它在循环之前等待查询完成。我意识到关于这个问题还有其他问题,但我不知道如何解决它。功能如下:

func stepsAllTime(completion: (Double, NSError?) -> () ) {
    var stopStart = true
    while stopStart {

        x += -1
        // The type of data we are requesting
        let sampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)
        var daysAgo = x
        var daysSince = x + 1
        var daysSinceNow = -1 * daysAgo
        checker = allTimeSteps.count

        // 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
            self.allTimeSteps.append(steps)
            println("New Sum:")
            println(self.allTimeStepsSum)
            println("Days Since Today:")
            println(daysSinceNow)
            if !(self.allTimeStepsTotal > self.allTimeStepsSum) {
                stopStart = false
            }
        }
        if allTimeStepsTotal > allTimeStepsSum {

            self.healthKitStore.executeQuery(stepQuery)

        }

    }

}

如何做到这一点?Swift中是否有某种“On Complete”函数?

您可以通过实现回调使程序等待查询完成

你可以在下面的博文中了解更多
我认为我的评论是准确的。 您可能想看看递归模式,类似这样:

import HealthKit

class Person {

    var nbSteps: Double = 0

    func fetchInfo() -> Void {
        let sampleType = HKSampleType()
        let samplePredicate: NSPredicate? = nil // Your predicate
        let stepQuery = HKSampleQuery(
            sampleType: sampleType,
            predicate: samplePredicate,
            limit: 0,
            sortDescriptors: nil) { (sampleQuery, object, error) -> Void in
                self.nbSteps += 1 // or the value you're looking to add

                dispatch_async(dispatch_get_main_queue(), { () -> Void in // Dispatch in order to keep things clean
                    self.fetchInfo()
                })
        }
    }

}

我只是想说清楚。。。您希望在执行stopStart过程时,在执行下一个
之前调用您的
stepQuery
完成闭包吗?如果您没有注意到,我的函数中已经有了这个闭包,但它没有帮助。