我想在 map 上向用户显示当前位置,我知道这不是即时任务。我想在 ASYNC 任务中调用我的 showCurrentLocation() 函数。我试图学习回调闭包,但我无法理解如何为此创建 ASYNC 任务。我知道这不是 stackoverflow 的最佳问题模板,但我不知道如何提出不同的问题。 感谢您的任何帮助。 有一个很好的编码。
最佳答案
过去我创建了一个名为 AsyncTask 的类用于您在此处描述的用途。
最近我为 swift 3 更新了它
它有两种泛型类型:
BGParam
- 执行时发送给任务的参数类型。
BGResult
- 后台计算结果的类型。
如果不需要其中之一(或两者),您可以将其设置为可选或忽略。
在代码示例中,我引用了您在 OP 和评论中提到的函数 showCurrentLocation()
和 getCurrentLocation()
。
BGParam
设置为 Int
& BGResults
设置为 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
关于ios - 在 Swift 2 中创建异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32177362/