ios - 在 Swift 2 中创建异步任务

标签 ios swift asynchronous callback closures

我想在 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/

相关文章:

c# - wpf 应用程序中的异步/等待方法

ios - 检索给定特定字段的 Firebase 数据

swift - 更新 Embed Swift 标准库设置

c# - 在 .net c# 中异步或并行地在数据库中保存记录

swift - DispatchQueue 同步块(synchronized block)与异步 block

uitableview - 'NSInvalidArgumentException',原因 : '-[UIImageView _isResizable] Exception with custom tableview cell

objective-c - MobFox 总是给我回复 "No inventory for ad request"

ios - 该 bundle 无效。 UIRequiredDeviceCapability 中提供了未知的设备功能值

ios - 在 Swift 中将 [NSObject, AnyObject] 转换为 [String, AnyObject]

ios - 访问核心数据镜像中的外部数据引用属性