0

我正在设置一个 iOS 8 应用程序来请求 Heath Kit Store 授权以共享类型。请求读/写屏幕显示正常,在选择完成后,我立即看到完成回调。在这个回调中,我正在推送一个新的视图控制器。我为以编程方式推送下一个视图控制器的代码设置了一个断点,它会立即被调用,但直到大约 10 秒后才会发生转换。

一些代码:

@IBAction func enable(sender: AnyObject) {
    let hkManager = HealthKitManager()
    hkManager.setupHealthStoreIfPossible { (success, error) -> Void in
        if let error = error {
            println("error = \(error)")
        } else {
            println("enable HK success = \(success)")
            self.nextStep()
        }
    }
}

func nextStep() {
        self.nav!.pushViewController(nextController, animated: true)
}


class HealthKitManager: NSObject {

    let healthStore: HKHealthStore!

    override init() {
        super.init()
        healthStore = HKHealthStore()
    }

    class func isHealthKitAvailable() -> Bool {
        return HKHealthStore.isHealthDataAvailable()
    }

    func setupHealthStoreIfPossible(completion: ((Bool, NSError!) -> Void)!) {
        if HealthKitManager.isHealthKitAvailable()
        {
            healthStore.requestAuthorizationToShareTypes(dataTypesToWrite(), readTypes: dataTypesToRead(), completion: { (success, error) -> Void in
                completion(success, error)
            })
        }
    }

    func dataTypesToWrite() -> NSSet {
        let runningType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)
        let stepType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)

        return NSSet(objects: runningType, stepType)
    }

    func dataTypesToRead() -> NSSet {
        let runningType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)
        let stepType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)
        let climbedType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierFlightsClimbed)

        return NSSet(objects: runningType, stepType, climbedType)
    }
}

关于导致过渡时间延迟的原因有什么想法吗?

4

1 回答 1

1

问题是完成块在后台队列中返回。我只是将转换调用放回主队列,如下所示:

hkManager.setupHealthStoreIfPossible { (success, error) -> Void in
        if let error = error {
            println("error = \(error)")
        } else {
            dispatch_async(dispatch_get_main_queue(), {
                println("enable HK success = \(success)")
                self.nextStep()
            });

        }
    }
}
于 2015-02-17T22:05:14.370 回答