2015-05-21 29 views
0

在我的應用程序中,我要求HealthKit訪問,並且工作正常。我獲得讀取步數和航班爬升的權限。在權限被授權後,我想將用戶發送到新的ViewController。這工作正常,但在ViewController的元素顯示爲5/6秒之後。我已經通過故事板將這些元素放入了ViewController。如果我添加代碼的元素,他們直接顯示。HealthKit訪問後訪問ViewController,但5秒後顯示元素

這怎麼可能?我想要在加載ViewController時直接顯示所有元素。

這是我的代碼,我用它來檢查,如果HealthKit授權和向用戶發送到下一個ViewController

healthManager.authorizeHealthKit { (authorized, error) -> Void in 
    if authorized { 
     let vc : AnyObject = self.storyboard!.instantiateViewControllerWithIdentifier("mainVC") 
     self.showViewController(vc as! UIViewController, sender: vc) 
    } else { 
     if error != nil { 
      println("\(error)") 
     } 
    } 
} 
+0

我不跟着你正在遇到的問題。在加載應用程序之前,您可能需要提取數據。有時Healthkit對響應速度很慢(大量數據),從而導致延遲。你可能想要加載它,然後在AppDelegate中推送視圖,然後將它傳遞給你的視圖。 – myusuf3

+0

延遲確實是問題所在。在用戶授予權限之前,我無法訪問HealthKit數據,並且在數據可訪問時我沒有獲得任何成功的任何東西。 –

回答

1

HealthKit documentation

用戶完成響應後,此方法在後臺隊列上調用其完成塊。

你應該做的主線程上的所有UI相關的東西,所以纔派遣它有

dispatch_async(dispatch_get_main_queue()) { 
    let vc : AnyObject = self.storyboard!.instantiateViewControllerWithIdentifier("mainVC") 
    self.showViewController(vc as! UIViewController, sender: vc) 
} 
+0

我看到在文檔中,但我把它放在錯誤的ViewController中,謝謝。 –