Ios 在Swift 2中创建异步任务

Ios 在Swift 2中创建异步任务,ios,swift,asynchronous,callback,closures,Ios,Swift,Asynchronous,Callback,Closures,我想向用户显示地图上的当前位置,我知道这不是即时任务。我想在异步任务中调用showCurrentLocation()函数。我试图学习回调闭包,但我不明白如何为此创建异步任务。我知道这不是stackoverflow的最佳问题模板,但我不知道如何才能提出不同的问题。 谢谢你的帮助。 有一种很好的编码方法。有一种叫做的技术,你可以用它来执行这样的任务 let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT dispatch_async(dispatch_get_g

我想向用户显示地图上的当前位置,我知道这不是即时任务。我想在异步任务中调用showCurrentLocation()函数。我试图学习回调闭包,但我不明白如何为此创建异步任务。我知道这不是stackoverflow的最佳问题模板,但我不知道如何才能提出不同的问题。 谢谢你的帮助。
有一种很好的编码方法。

有一种叫做的技术,你可以用它来执行这样的任务

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
    }
}

在过去,我创建了一个为使用目的而调用的类,就像您在这里描述的那样

最近我更新了它的swift 3

它有两种通用类型:

BGParam
-执行时发送到任务的参数类型

BGResult
-背景计算结果的类型

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

在代码示例中,我引用了您在OP&comments中提到的函数
showCurrentLocation()
&
getCurrentLocation()
BGParam
设置为
Int
bgfresults
设置为
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。我想知道,我可以用闭包创建自定义异步任务吗?想想这种情况。假设我们有一个getCurrentLocation()函数,它返回userCurrentLocation.latitude和longitude。我不能立即获取值,所以我必须使用回调机制。它应该告诉我我们找到了位置,并且有结果,所以我可以在主线程中使用它们。如果将getCurrentLocation函数放在后台线程中。它解决了我的问题吗?它会像回调机制一样工作吗?非常感谢。使用全局队列不是最好的主意。。。你知道是谁用的。在这里,创建自己的并发队列是一个更好的解决方案。