在Swift 2中创buildASYNC任务

我想在地图上显示用户当前的位置,我知道这不是即时的任务。 我想在ASYNC任务中调用我的showCurrentLocation()函数。 我试图学习callback闭包,但我不明白如何创buildASYNC任务。 我知道这不是一个最好的问题模板为stackoverflow,但我不知道我怎么能不同地问。 感谢您的任何帮助。 有一个很好的编码。

在过去,我做了一个名为AsyncTask的类,用于您在此处描述的用途。

最近我更新了快3

它有2个通用types:

BGParam – 执行时发送给任务的参数的types。

BGResult – 背景计算结果的types。

如果其中一个(或两个)不需要,您可以将其设置为可选或只是忽略。

在代码示例中,我引用您在OP和注释中提到的函数showCurrentLocation()getCurrentLocation()BGParam设置为IntBGResults设置为CLLocationCoordinate2D

 AsyncTask(backgroundTask: {(param:Int)->CLLocationCoordinate2D in print(param);//prints the Int value passed from .execute(1) return self.getCurrentLocation();//the function you mentioned in comment }, afterTask: {(coords)in //use latitude & longitude at UI-Main thread print("latitude: \(coords.latitude)"); print("latitude: \(coords.longitude)"); self.showCurrentLocation(coords);//function you mentioned in OP }).execute(1);//pass Int value 1 to backgroundTask 

有一种叫做GCD(Grand Central Dispatch)的技术 ,可以用来执行这些任务。

 let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT dispatch_async(dispatch_get_global_queue(priority, 0)) { // do some task dispatch_async(dispatch_get_main_queue()) { // update some UI } }