func application(application: UIApplication!,dIDFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool { self.API.signInWithToken(emailstring,token: authtokenstring) { (object: AnyObject?,error:String?) in if(object != nil){ self.user = object as? User // go straight to the home vIEw if auth succeeded var rootVIEwController = self.window!.rootVIEwController as UINavigationController let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil) var homeVIEwController = mainStoryboard.instantiateVIEwControllerWithIDentifIEr("HomeVIEwController") as HomeVIEwControllerenter // code here rootVIEwController.pushVIEwController(homeVIEwController,animated: true) } } return true}
API.signInWithToken是一个用Alamofire进行的异步调用,我想在func应用程序的最后一端返回true之前等待它的完成.
解决方法Note: You should not do it this way,as it blocks the thread. See Nate’s comment above for a better way.
有一种方法可以等待异步调用使用GCD完成.代码如下所示
var semaphore = dispatch_semaphore_create(0)performSomeAsyncTask { ... dispatch_semaphore_signal(semaphore)}dispatch_semaphore_wait(semaphore,disPATCH_TIME_FOREVER)dispatch_release(semaphore)
维基百科有一个OK article,如果您对信号量一无所知.
总结以上是内存溢出为你收集整理的等到异步api调用完成 – Swift / IOS全部内容,希望文章能够帮你解决等到异步api调用完成 – Swift / IOS所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)